【发布时间】:2016-01-12 22:21:51
【问题描述】:
do {
input = scan.nextInt();
//calculates minimum
if (input < min) {
min = input;
}
//calculates sum of even integers
else if (input % 2 != 1) {
sumeven = sumeven+1;
}
//calculates sum of negative integers
if (input < 0) {
sumnegative += input;
}
} while (input != 0);
所以程序让用户输入一系列输入。我遇到的问题是,当我输入一个小于 0 的数字时。程序应该告诉我有多少个偶数。所以问题是当用户输入一个类似的序列时:
-1 -2 -45 -90 1 23 678 90 0。
程序会告诉我有 3 个偶数。由于某种原因,它不会将负数计为偶数?
【问题讨论】:
-
您是否在调试器中遍历了代码以查看
input%2的计算结果? -
为什么你的第二个
if上有一个else? -
另请参阅之前的问题:Mod in Java produces negative numbers
-
非常感谢!这是因为第二个 if 的 else 。你让我开心。
标签: java