在这个问题中,我需要让用户输入他们要输入的整数个数。然后,他们将手动输入每个数字。最后,程序计算所有偶数输入的总和和最大值。
我在启动值时遇到了麻烦。如果我将它们设置为零,如果所有数字都是负偶数,就会出现问题。
import java.util.*;
public class Exercise07 {
public static void main(String[] args) {
Scanner thing = new Scanner(System.in);
evenSumMax(thing);
}
private static void evenSumMax(Scanner stuff) {
System.out.print("How many integers?");
int times = stuff.nextInt();
int evenSum = 0;
int evenMax = 0;
System.out.println("Number 1 : ");
int value = stuff.nextInt();
if (value % 2 == 0) {
evenSum += value;
evenMax = value;
}
for (int i = 2 ; i <= times; i++) {
System.out.print("Number " + i + " : ");
value = stuff.nextInt();
if (value % 2 == 0) { // if even
evenSum += value;
if (evenMax < value) {
evenMax = value;
}
}
}
System.out.println("Even sum = " + evenSum + ", even max = " + evenMax);
}
}