简体   繁体   English

Java中如何使用逻辑运算符

[英]How to use logical operator in Java

How does java evaluate the following code with the logical operator in java? java 如何使用 java 中的逻辑运算符评估以下代码?

public class ApaBoleh{
  public static void main(String[]args){
      for(int i=1;i<=100;i++){
          if(i%3==0){
              System.out.print("Apa,");
          }else if (i%5==0){
              System.out.print("Boleh,");
          }else if ((i%3==0)&&(i%5==0)){
              System.out.print("ApaBoleh,");
          }
         System.out.print(i+",");
      }
  }
}

when I run this code, the following line doesn't run }else if ((i%3==0)&&(i%5==0)){ .当我运行此代码时,以下行不会运行}else if ((i%3==0)&&(i%5==0)){

It's because you're using else to exclude anything prior to that condition that resolves to true.这是因为您使用else来排除该条件解析为 true 之前的任何内容。 Try checking the most exclusive case first:首先尝试检查最独特的情况:

if (( i%3 == 0 ) && ( i%5 == 0 )){
    System.out.print("ApaBoleh,");
}else if ( i%3 == 0){
    System.out.print("Apa,");
}else if ( i%5 == 0){
    System.out.print("Boleh,");
}

System.out.print(i+",");

You have an else where both preceding if (s) are true ;你有一个else 前面的if (s) 都是true only the first is entered.只输入第一个。 I would save the tests to local variables because repeating all of those modulus operations is not very clean.我会将测试保存到局部变量,因为重复所有这些模运算不是很干净。 Like,喜欢,

for (int i = 1; i <= 100; i++) {
    boolean mod3 = i % 3 == 0, mod5 = i % 5 == 0;
    if (mod3 && mod5) {
        System.out.print("ApaBoleh,");
    } else if (mod3) {
        System.out.print("Apa,");
    } else if (mod5) {
        System.out.print("Boleh,");
    }
    System.out.print(i + ",");
}

See also The FizzBuzz challenge .另请参阅FizzBuzz 挑战

Before run运行前

else if ((i%3==0)&&(i%5==0)){ System.out.print("ApaBoleh,");}

it runs它运行

if(i%3==0){
    System.out.print("Apa,");
}else if (i%5==0){
    System.out.print("Boleh,");

so you have to put所以你必须把

else if ((i%3==0)&&(i%5==0)){ System.out.print("ApaBoleh,");}

before above two以上两个之前

Of course it'll not run, the moment one of your previous two if conditions evaluate to true , you'll never get to that if block.当然它不会运行,当你的前两个 if 条件之一评估为true时,你永远不会遇到那个 if 块。

For example, if i = 3 then your first condition if(i%3==0) will evaluate to true and your print out Apa , since the rest of the conditions are else if the java code will stop processing and go to the next loop iteration.例如,如果i = 3 ,那么您的第一个条件if(i%3==0)将评估为true并且您的打印输出Apa ,因为条件的 rest 是else if java 代码将停止处理并且 Z34D1F725A07423FE1C889F448B33D21F46Z 代码将停止处理并且 Z34D1F725A07423FE1C889F448B33D21F46Z 将停止处理并且 Z34D1F725A07423FE1C889F448B33D21F46Z 到下一个 Z34D1F91FB2E5A8B9循环迭代。 Similarly, if i = 5 then first condition will evaluate to false and the second condition if(i%5==0) will evaluate to true and will not continue to the next else if同样,如果i = 5 ,则第一个条件将评估为false ,第二个条件if(i%5==0)将评估为true ,并且不会继续下一个else if

Hence if either of your previous conditions evaluate to true you'll not get to your third condition.因此,如果您之前的任何一个条件评估为true ,您将无法达到第三个条件。

Apart from this, if you had to remove the preceding two if statemets and only evaluate that condition as follows:除此之外,如果您必须删除前两个if语句并仅按如下方式评估该条件:

public class ApaBoleh{
  public static void main(String[]args){
    for(int i=1;i<=100;i++){
      if ((i%3==0)&&(i%5==0)){
        System.out.print("ApaBoleh,");
      }
     System.out.print(i+",");
    }
  }
}

Then if i = 3 it'll evaluate to false because it'll evaluate i%3==0 as true and then go on to evaluate i%5==0 as false ( true and false = false ).然后,如果i = 3它将评估为false ,因为它会将i%3==0评估为true ,然后 go 将i%5==0评估为falsetruefalse = false )。

Then if i = 15 it'll evaluate to true because it'll evaluate i%3==0 as true and then go on to evaluate i%5==0 as true ( true and true = false ).然后,如果i = 15它将评估为true ,因为它将评估i%3==0true ,然后 go 将i%5==0评估为truetruetrue = false )。

The conditions inside your }else if ((i%3==0)&&(i%5==0)){ evaluates as true whenever i%3 is equal to 0 or i%5 is equal to 0 so, if you really want }else if ((i%3==0)&&(i%5==0)){ to run you could either use }if ((i%3==0)&&(i%5==0)){ instead of it or you should at least place it above }else if (i%5==0){ or you could use it like this like the others told before me: }else if ((i%3==0)&&(i%5==0)){中的条件在 i%3 等于 0 或 i%5 等于 0 时评估为真,因此,如果您真的想要}else if ((i%3==0)&&(i%5==0)){运行你可以使用}if ((i%3==0)&&(i%5==0)){而不是它,或者您至少应该将它放在}else if (i%5==0){之上,或者您可以像其他人告诉我的那样使用它:

public class ApaBoleh{
    public static void main(String[]args){
        for(int i=1;i<=100;i++){
            if ((i%3==0)&&(i%5==0)){
                System.out.print("ApaBoleh,");
            }else if (i%5==0){
                System.out.print("Boleh,");
            }else if(i%3==0){
                System.out.print("Apa,");
            }
            System.out.print(i+",");
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM