Hello everyone!
Once again I need some help on this program. I need to write a program that reads integers, finds the largest of them, and counts how many times that number occurs. The program should end when 0 is input. My teacher gave us a hint, incase this helps "Maintain two variables, max and count. max stores the current max number, and count stores its occurrences. Initially, assign the first number to max and count to 1. Compare each subsequent number with max. If the number is greater than max, assign it to max and reset count to 1. If the number is equal to max, increment count by 1."
Sample 1:
Enter numbers: 3 5 2 5 5 5 0
The largest number is 5
The occurrence count of the largest number 4
Ok, so heres what I have so far.
import java.util.Scanner;
public class occurence
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int max;
int num=0;
int counter=1;
max = input.nextInt();
do
{
num = input.nextInt();
}while(num != 0);
int largest = max(num);
if(max<num){
counter=1;
}
else if(max==num){
counter++;
}
System.out.println("there are " + counter + " of the largest number");
}
}
I have no idea what im doing hahaha. However I guess my main problem is how to compare max to the next subsequent numbers.