First error:
System.out.println("the biggest value is: "+n+" and it is occurs "+H+" times");}}
The n
is your TryCount. Should be:
System.out.println("the biggest value is: "+y+" and it is occurs "+H+" times");}}
Second error:
You are increasing the count of the “highest” number: else if(y==f) H++;
– but you are NOT taking into account, what should happen, when that changes? So, entering 1,1,1,1,1,1,2, will give you “7 occurences of 2” – thats wrong.
You need to “reset” (Set to “1”) the “Highest-Occurence-Count”, when a new Highest number is recorded:
if ( f>y){
y=f;
H = 1;
}
Third error: Already fixed above: It should be f>y
not f>H
Hint: give your variables meaningfull names – so you dont mess up that easy:
import java.util.*;
public class numbers{
public static void main(String args[]){;
Scanner input=new Scanner (System.in);
int runs=0;
int highestCount=0;
int highestValue=0;
System.out.println("please Enter 10 numbers:");
while (runs<10){
int inputValue=input.nextInt();
if ( inputValue>highestValue){
highestValue=inputValue;
highestCount = 1;
}
else if(inputValue==highestValue){
highestCount++;
}
}
System.out.println("the biggest value is: "+highestValue+" and it is occurs "+highestCount+" times");
}
}
much easier to read, isn’t it?
0
solved Searching and Counting Greatest Values [closed]