import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Enter numbers, input ends with 0: ");
int max = 0;
int count = 0;
while (scanner.nextInt() != 0 )
{
if (scanner.nextInt() > max)
{
max = scanner.nextInt();
count = 1;
}
else if (scanner.nextInt() == max)
{
count++;
}
}
if (max == 0 && count == 0)
{
System.out.println("No numbers are entered except zero.");
}
else
{
System.out.println("The largest number is " + max);
System.out.println("The occurence count of the largest number is " + count);
}
}
}
The above code is what I have managed to create thus far in an attempt to create a program that will identify the largest number, and also take a count of how many times that number is listed. I would also like the number 0 to trigger the end of the input sequence.
For example, if I type:
2 3 9 4 5 9 3 3 0
I should receive a max of 9 and a count of 2.
The code seems to work sometimes, and other times does not return any output when a sequence ending in 0 is entered. I also get an incorrect count but correct max other times.
Please help me understand what is going haywire with my code.