【问题标题】:Java not reading "&&" statements properly?Java没有正确读取“&&”语句?
【发布时间】:2015-09-24 11:48:21
【问题描述】:

我的 Java 代码有问题。具体来说,我的一个包含 && 的 if 语句没有像我期望的那样对某些输入返回 True。

有问题的sn-p:

if (num%2==1 && num < 0) {       //why is not reading this statement?
    negodd ++;
}

示例输入和输出与预期输出:

 Enter any number to continue. Enter 0 to stop :
 1
 2
-2
-1
 0   // not counted as a number since it is a stop function.

Output of my code.                     What it should be.
You Entered 4 numbers :               You Entered 4 numbers :
1 negative even                         1 negative even
1 positive even                         1 positive even                         
0 negative odd                          1 negative odd  <--should read the 1
1 positive odd                          1 positive odd

完整的代码以防万一:

 import java.util.Scanner;
 public class stupid {
     public static void main(String[] args) {
         Scanner x = new Scanner(System.in);
         int num = 0;
         int negodd = 0, count = 0, posseven = 0;
         int possodd = 0; int negeven=0;

         System.out.println("Enter any number to continue. Enter 0 to stop : ");
         num = x.nextInt();

         if(num==0){
             System.out.print("You immediately stop");
             System.exit(0);
         }

         while (num != 0) {
             count ++;
             if (num%2==1 && num > 0) {
                 possodd ++;
             } 
             if (num%2==1 && num < 0) {       //why is not reading this statement?
                 negodd ++;
             }
             if (num%2==0 && num > 0) {
                 posseven ++;
             }
             if (num%2==0 && num < 0) {
                 negeven++;
             }
             num = x.nextInt();
         }
         System.out.printf("You Entered %d numbers\n",count);
         System.out.printf("%d negative even \n",negeven);
         System.out.printf("%d positive even\n",posseven);
         System.out.printf("%d negative odd\n",negodd);
         System.out.printf("%d positive odd\n",possodd);
    }
}

提前致谢!

【问题讨论】:

  • 与其调用System.exit(0),不如尝试使用return简单地从方法返回。
  • 嗯,我还是编程新手,如果我在我的程序中加入新东西,我的教授不会将我的工作记录为正确的。在这个网站上仍然学到了很多东西。 :)
  • 读取正确,但num % 2 永远不会是1 的负数。

标签: java logical-operators


【解决方案1】:

对负数使用模运算符会得到与您想象的不同的结果。

1 % 2 == 1
2 % 2 == 0
-2 % 2 == 0
-1 % 2 == -1

为了得到你想要的结果,你可以用num % 2 == 0num % 2 != 0替换你的模测试。

【讨论】:

  • 好的,还有一件事我如何打印这一行“你输入了一个正的偶数”?当用户输入 2 然后输入 0。
  • 和其他的一样,比如 -1 然后 0 然后它应该打印“你输入了一个负奇数”。
  • 好吧,因为您的num 变量在您输入0 时被覆盖,您必须将之前的值存储在一个新变量中。当您离开 while 循环时,您必须检查该变量是正数还是负数。
  • 你能解释一下,比如我把 count =1 然后如果 num > 0?这就是我得到的逻辑错误,我认为它没有执行 num
  • 其实应该是num % 2 == 0num % 2 != 0,不需要Math.abs BTW Math.abs可以返回负数;)
【解决方案2】:
1 % 2 == 1
2 % 2 == 0
-2 % 2 == 0
-1 % 2 == -1

这里,-1%2 的结果不是 1。因此,它不会增加 negodd 变量的值。

JAVA 中,负数模将给出与正数模相同的结果,但带有一个负号。 0 是中性的,因此它不会有任何符号。

【讨论】:

    【解决方案3】:

    -1 % 2 将返回 -1,而不是 1,其中 -2 % 2 将返回 0。

    【讨论】:

      猜你喜欢
      • 2021-09-07
      • 1970-01-01
      • 2018-08-03
      • 2015-01-10
      • 2021-08-03
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      相关资源
      最近更新 更多