Java和和或运算符不工作

时间:2014-12-30 02:50:05

标签: java operators conditional

当满足两个条件时,我使用以下语句来摆脱循环:

while (true)
    {
        if (uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
                || uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4) 
                &&
                uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)){
            break;
        }

当一个或两个&&条件得到满足。但是,当两个条件都为真时,我编写了代码来打破循环。

上述陈述中是否缺少某些内容? 问候, Shei7141。

3 个答案:

答案 0 :(得分:3)

将它们包装在括号中

if ( (uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
                || uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4))
&&
(uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)) )

即使提出HashSet正确的答案,这样做也会很干净,而且效率也很高

answers1Set.contains(uAnswer1) && answers2Set.contains(uAnswer2)  

答案 1 :(得分:0)

上面的代码显示 uAnswer1.equals(answerB4)&& uAnswer2.equals(answerS1)处于 AND 状态

while (true)
    {
        if ((uAnswer1.equals(answerB1) || uAnswer1.equals(answerB2)
                || uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4)) 

                &&

                (uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2))){
            break;
        }

答案 2 :(得分:0)

while(true)     {         if((uAnswer1.equals(answerB1)|| uAnswer1.equals(answerB2)                 || uAnswer1.equals(answerB3)|| uAnswer1.equals(answerB4))                 &安培;&安培;                 (uAnswer2.equals(answerS1)|| uAnswer2.equals(answerS2)))             打破;         }