I am trying to find maximum number from a loop and it’s number of occurrences. Here is the code I have written so far.
public class MaxNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int max = 0;
int number = 0;
int count_max = 0;
while (true) {
number = input.nextInt();
if (number == 0) {
break;
}
number = max;
if (max > number) {
max=number;
count_max++;
}
}
System.out.println("Max Number: " + number);
System.out.println("Occurences: " + count_max);
}
I getting only zero in the output, any logical error I am making?
This is my version of what I think you are trying to achieve, it is mostly working, please note that you have to start typing in numbers if you start with a string it fails as you describe, also note numbers larger then
longget ignored or ‘trimmed’