小编jut*_*eni的帖子

必需的布尔值,找到int?

我还在学习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.当然,编译失败了.

java ternary-operator

1
推荐指数
1
解决办法
825
查看次数

在三元运算符声明中非法开始表达

public class V0206 {

    public static void main(String[] args) {

            java.util.Scanner sc = new java.util.Scanner(System.in);

            int x = sc.nextInt();
            int y = 400;
            int z = 100;
            int q = 4;
            int rest =(int)(x % y);
            int rest2 = (int)(x % z);
            int rest3 = (int) (x % q);

            String result = (rest3 == 0 && rest2 != 0 || rest == 0 && rest2 == 0 ) ? "Leap year" :  "Not leap year";);
            if (result = true) {System.out.println("Leap …
Run Code Online (Sandbox Code Playgroud)

java if-statement ternary-operator

0
推荐指数
1
解决办法
809
查看次数

标签 统计

java ×2

ternary-operator ×2

if-statement ×1