字符串的 if-else 语句
If-else statement for a string
我正在尝试编写一个条件语句,如果满足条件,则字符串 oranges ="yes please"。如果不是,那么它应该说 "no, thanks"。我打算稍后在像 System.out.println("y"+ "m" + "d" + "oranges") 这样的打印语句中使用字符串 oranges。 y、m 和 d 是用户输入。这是我的代码的相关部分。我做错了什么?
(经验水平:前几天Java刚开始)
String oranges;
if ((y > 2777) && (m > 2) && (d > 22)){
String oranges = "yes please";
}
else
{String oranges = "no, thanks";
}
您的代码无法成功编译,您已声明 oranges
两次。看一下全局变量和局部变量。右边应该是这样的:
String oranges;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else{
oranges = "no, thanks";
}
System.out.println(oranges);
请尝试:
String oranges,afterToday;
int x, y , d; //here code goes to get user input
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}else{
afterToday = "no, thanks";
}
System.out.println("Y = "+y "M = "+m + "D ="+d + "Oranges: "+oranges);
问题是重新声明 orange 的数据类型,第一个声明没问题,然后第二个问题是您的输出不应在引号中包含 oranges,因为那样不会输出 oranges 变量的值。添加引号将导致橙子作为字符串输出。
这是我刚才解释的例子
public class JavaApplication1 {
public static void main(String[] args) {
String oranges = "Any thing you want";
int y = 0;
int m = 0;
int d = 0;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else{
oranges = "no, thanks";
}
System.out.println(y+ m + d + oranges);
}
}
您也可以走酷路,尝试三元运算符方法
String oranges = ((y > 2777) && (m > 2) && (d > 22)) ? "yes please" : "no, thanks";
语法是 ?=then, := else
在这种方法中,您只需要一个字符串变量。
快乐 Java 编码!
您应该使用您的代码作为
String oranges="";
//declare y,m and d and initialize with some value or read data from user
//int y,d,m;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else
{
oranges = "no, thanks";
}
System.out.println(y+" "+m+" "+d+" "+oranges);
这里有一些代码,您可以使用它们来实现您的 objective 并包括用于打印的 System.out.printf(String, Object...)
方法(与单独的 System.out.println(String)
相比,此方法对输出格式提供了更多的控制; printf
文档可以看h̲e̲r̲e̲):
public class Solution {
public static void main(String[] args) {
// Code in which the variables y, m, and d are created and assigned proper values goes here.
String oranges = ((y > 2777) && (m > 2) && (d > 22)) ? "yes please" : "no, thanks";
System.out.printf("%d %d %d %s", y, m, d, oranges);
}
}
我正在尝试编写一个条件语句,如果满足条件,则字符串 oranges ="yes please"。如果不是,那么它应该说 "no, thanks"。我打算稍后在像 System.out.println("y"+ "m" + "d" + "oranges") 这样的打印语句中使用字符串 oranges。 y、m 和 d 是用户输入。这是我的代码的相关部分。我做错了什么?
(经验水平:前几天Java刚开始)
String oranges;
if ((y > 2777) && (m > 2) && (d > 22)){
String oranges = "yes please";
}
else
{String oranges = "no, thanks";
}
您的代码无法成功编译,您已声明 oranges
两次。看一下全局变量和局部变量。右边应该是这样的:
String oranges;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else{
oranges = "no, thanks";
}
System.out.println(oranges);
请尝试:
String oranges,afterToday;
int x, y , d; //here code goes to get user input
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}else{
afterToday = "no, thanks";
}
System.out.println("Y = "+y "M = "+m + "D ="+d + "Oranges: "+oranges);
问题是重新声明 orange 的数据类型,第一个声明没问题,然后第二个问题是您的输出不应在引号中包含 oranges,因为那样不会输出 oranges 变量的值。添加引号将导致橙子作为字符串输出。
这是我刚才解释的例子
public class JavaApplication1 {
public static void main(String[] args) {
String oranges = "Any thing you want";
int y = 0;
int m = 0;
int d = 0;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else{
oranges = "no, thanks";
}
System.out.println(y+ m + d + oranges);
}
}
您也可以走酷路,尝试三元运算符方法
String oranges = ((y > 2777) && (m > 2) && (d > 22)) ? "yes please" : "no, thanks";
语法是 ?=then, := else
在这种方法中,您只需要一个字符串变量。
快乐 Java 编码!
您应该使用您的代码作为
String oranges="";
//declare y,m and d and initialize with some value or read data from user
//int y,d,m;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else
{
oranges = "no, thanks";
}
System.out.println(y+" "+m+" "+d+" "+oranges);
这里有一些代码,您可以使用它们来实现您的 objective 并包括用于打印的 System.out.printf(String, Object...)
方法(与单独的 System.out.println(String)
相比,此方法对输出格式提供了更多的控制; printf
文档可以看h̲e̲r̲e̲):
public class Solution {
public static void main(String[] args) {
// Code in which the variables y, m, and d are created and assigned proper values goes here.
String oranges = ((y > 2777) && (m > 2) && (d > 22)) ? "yes please" : "no, thanks";
System.out.printf("%d %d %d %s", y, m, d, oranges);
}
}