The assignment instructions are so: Write a method named evenSum that prompts the user for many integers and print the total even sum and maximum of the even numbers. You may assume that the user types at least one non-negative even integer. I have everything written correctly except for one last part.
That part is determining the number with the highest value that is also an even number. So, I have it in the body of an if statement (numbers % 2 == 0). How do I figure out the maximum number? By the way, I can’t use arrays and I’ve tried Math.max but I’m not sure how to use it to determine the max.
public void evenSum()
{
int sumamount = 0;
int evenMax = 0;
int numberOfInputs = 0;
int numbers = 0;
Scanner in = new Scanner(System.in);
System.out.print("how many integers? ");
numberOfInputs = in.nextInt();
for(int i = 0; i < numberOfInputs; i++)
{
System.out.print("next integer? ");
numbers = in.nextInt();
if(numbers % 2 == 0)
{
sumamount += numbers;
if(numbers > numbers)
{
evenMax = numbers
}
}
else
numbers = 0;
}
System.out.print("even sum = ");
System.out.println(sumamount);
System.out.print("even max = ");
System.out.println(evenMax);
}
This line is the culprit:
The condition should be
Also, it may be better to initialize evenMax to
-1(an odd number) and handle the case where it is-1and initialize with the first input appropriately (i.e first even input). This will handle the cases where all the inputs are negative even numbers.