jut*_*eni 1 java ternary-operator
我还在学习Java ....
我的任务是编写一个程序,它划分两个双打,但在显示除法结果之前,必须说它们是否可分开(没有休息).我必须使用三元运算符.
码:
public class exercise {
public static void main(String[] args){
double x = 8.4;
double y = 4.2;
double z = (int)(x % y);
String result = (z>0) ? "not dividible" : "dividible";
System.out.println("This operation is: " + result);
System.out.println("The result of dividing is: " + (x/y));
}
}
Run Code Online (Sandbox Code Playgroud)
编译器说"String result =(z> 0)?....."它需要布尔值,并且它找到了int.当然,编译失败了.
你错过了)这句话的最后一个结束括号().
System.out.println("The result of dividing is: " + (x/y);
Run Code Online (Sandbox Code Playgroud)
它应该是:
System.out.println("The result of dividing is: " + (x/y));
?
Run Code Online (Sandbox Code Playgroud)
所有其他的东西都为我编译.