我目前堅持MOOC練習36.我的問題是我無法讓我的程序存儲奇數和偶數。我知道我需要使用模數運算符%2,所以程序可以找到提醒是否爲0 - 1是一個偶數或奇數。MOOC循環結束記憶
// program in this project exercises 36.1-36.5
// actually this is just one program that is split in many parts
Scanner reader = new Scanner(System.in);
System.out.println("Type numbers: ");
int tal; //Min reader funktion
int sum = 0;
int numbersTyped = 0;
double average = 0.0;
int even = 0;
int odd = 0;
while (true) {
tal = Integer.parseInt(reader.nextLine());
if (tal == -1){
System.out.println("Thank you and see you later!");
System.out.println("The sum is: " + sum);
System.out.println("How many numbers: " + numbersTyped);
System.out.println("Average: " + average);
System.out.println("Even numbers: " + even);
System.out.println("Odd numbers: " + odd);
break;
}
if (tal >= 0){
sum += tal;
numbersTyped++;
average = (double) sum/(double) numbersTyped;
} else if (tal > 0){
tal %= 2;
even = tal;
} else if (tal > 1) {
tal %= 2;
odd = tal;
}
}
}
[整數司Java中(可能的重複https://stackoverflow.com/questions/7220681/division-of-integers-in-java ) –