代码如下:public class Test
{
public static void main(String[] args)
{
int x = args[0];
(x == 5) ? "yes" : "no";
}
}为什么用cmd编译时会出现这样的错误提示:(x == 5) ? "yes" : "no";
^ not a statement难道这个问号也跟汉字一样,不被支持?
{
public static void main(String[] args)
{
int x = args[0];
(x == 5) ? "yes" : "no";
}
}为什么用cmd编译时会出现这样的错误提示:(x == 5) ? "yes" : "no";
^ not a statement难道这个问号也跟汉字一样,不被支持?
是三目运算符的结果只能作为一个值赋给一个变量。类似于下面的例子: public static void main(String args[]) {
int x = 0;
5; // 这个5能作为一个statement吗?
}
public class Test
{
public static void main(String[] args)
{
int x = Integer.parseInt(args[0]);
String s = x == 5 ? "yes" : "no"; //这里,好像不能单独用x == 5 ? "yes" : "no"吧,要将它赋给一个variable
}
}
这个在C/C++中正确的,在JAVA中对表达式有特定的要求.即: 表达式E;
要形成一个表达式语句,则表达式E必须只能是:赋值表达式,自增++表达式,自减--表达式,方法调用表达式,new 表达式.