布尔前pression编译错误 [英] compilation error on boolean expression

查看:99
本文介绍了布尔前pression编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Java的code的一个片段:

 静态布尔值的; //获取假
静态布尔B:
静态布尔℃;    公共无效printA(){        布尔布尔=(A =真)|| (B =真)及&放大器; (C = TRUE);
         System.out.print(A +,+ B +,+ C);
    }

这并不编译,什么是概率?错误:在这条线的多个标记;在布尔变量行语法错误。
我希望它打印真,假,真
虽然根据我的教程书它打印真,假,假

我的理解进行短路,但在的情况下,与放;&安培;双方需要进行评估。这不是一门功课,我学习Java。
干杯


解决方案

 (A =真)|| (B =真)及&放大器; (C = TRUE);

相当于: -

 (A =真)|| ((二=真)及及(C =真));

由于(A = TRUE)进行评估,以<​​code>真正,因此第二个前pression不评估,因为你正在使用的短路操作符(||)的存在。

和因此最后两个分配不会发生。和 b的值 C 仍然

注意: - 短路运营商 - &放大器;&安培; || ,不会进一步评估是否一定结果可以通过previous评价中获得。

所以: -


  • A和&安培; b 不会评价的的,如果的的是假的。


  • A || b 不会评价的的,如果的的是真实的。


This is a snippet of Java code:

static boolean a; // gets false
static boolean b; 
static boolean c;

    public void printA(){

         boolean bool = (a = true) || (b = true) && (c = true);    
         System.out.print(a + ", " + b + ", " + c);     
    }

It does not compile, what is the prob? Error: multiple markers on this line; syntax error on the line of 'bool' variable. I expect it to print true, false, true. Although according to my tutorial books it prints true, false, false.

I understand it performs short-circuiting but in case of && both sides needs to be evaluated. That is not a homework, I am learning Java. Cheers

解决方案

(a = true) || (b = true) && (c = true);

is equivalent to: -

(a = true) || ((b = true) && (c = true));

Since (a = true) is evaluated to true, hence the 2nd expression is not evaluated, since you are using short-circuit operator (||) there.

And hence the last two assignment does not happen. And the values of b and c remain false.

Note: - Short-circuit operators - && and ||, does not evaluate further if a certain result can be obtained by previous evaluation.

So: -

  • a && b will not evaluate b, if a is false.

  • a || b will not evaluate b, if a is true.

这篇关于布尔前pression编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆