感谢您查看我的代码。我正在学习 Java,遇到了一个让我发疯的问题。
/*
* The loop reads positive integers from standard input and that
* terminates when it reads an integer that is not positive. After the loop
* terminates, it prints out, separated by a space and on a single line, the
* sum of all the even integers read and the sum of all the odd integers
*/
问题是变量没有添加!我知道我的语法很好。我认为对于循环的工作原理和添加方式,我不了解 Java 语言的某些内容。
import java.util.Scanner;
class Testing2 {
public static void main(String[] args) {
int sumP = 0;
int sumO = 0;
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a positive or negative integer: ");
while ((stdin.nextInt()) >= 0) {
if (stdin.nextInt() % 2 == 0)
sumP += stdin.nextInt();
else
sumO += stdin.nextInt();
}
System.out.println(sumP + " " + sumO);
stdin.close();
}
};