Reputation: 45
Im trying to count how many times each number between 0 to 9 was typed in a loop, when press -1 it will stop the loop, I made it work with the long term, Also I would have to print which of the numbers was typed the most. For now I only have the couting code:
int pickedNum,counter_0=0,counter_1=0,counter_2=0,counter_3=0,counter_4=0,counter_5=0,counter_6=0,counter_7=0,counter_8=0,counter_9=0;
do{
System.out.println("Please enter a numbr between 0-9 , -1 to exit:");
pickedNum=s.nextInt();
if(pickedNum==0){
counter_0++;
}
if(pickedNum==1){
counter_1++;
}
if(pickedNum==2){
counter_2++;
}
if(pickedNum==3){
counter_3++;
}
if(pickedNum==4){
counter_4++;
}
if(pickedNum==5){
counter_5++;
}
if(pickedNum==6){
counter_6++;
}
if(pickedNum==7){
counter_7++;
}
if(pickedNum==8){
counter_8++;
}
if(pickedNum==9){
counter_9++;
}
}
while(pickedNum != -1);
System.out.printf("The number 0 appears: %d \n"
+ "The number 1 appears: %d \n"
+ "The number 2 appears: %d \n"
+ "The number 3 appears: %d \n"
+ "The number 4 appears: %d \n"
+ "The number 5 appears: %d \n"
+ "The number 6 appears: %d \n"
+ "The number 7 appears: %d \n"
+ "The number 8 appears: %d \n"
+ "The number 9 appears: %d \n",
counter_0,counter_1,counter_2,
counter_3,counter_4,counter_5,
counter_6,counter_7,counter_8,counter_9);
as you can see, It works fine, But I know there is a way better option to do this thing. And as I mentioned, I also need to print the largest number counts(which number was type the most), and It will take a long way to reach that with my way of programming this. I would like to get any tip or notes to improve this to work easier and faster. Any notes would be appriciated.
EDIT: Forgot to mention to use only basics, and not advanced methods, array and loops are fine.
Upvotes: 1
Views: 789
Reputation: 393
Don't forget that you can have more than one number that was typed the most, take a look at this :
public static void main(String[] args) throws ParseException {
int pickedNum, highestCounter = 0;
Scanner s = new Scanner(System.in);
int count[] = new int[10];
while (true) {
System.out.println("Please enter a numbr between 0-9 , -1 to exit:");
pickedNum = s.nextInt();
if (pickedNum != -1) {
count[pickedNum]++;
} else {
break;
}
}
int index1 = 0;
for (int i : count) {
System.out.println("The number " + index1 + " appears : " + i);
index1++;
}
// Finding the number was typed the most
for (int counter : count) {
if (counter > highestCounter) {
highestCounter = counter;
}
}
// In case if you have more than one number that was typed the most
Integer[] indexes = new Integer[10];
int index2 = 0;
for (int counter : count) {
if (highestCounter == counter) {
indexes[index2] = index2;
}
index2++;
}
System.out.print("The number(s) was typed the most is/are : ");
for (Integer i : indexes) {
if (i != null) {
System.out.print(i + ", ");
}
}
}
And the output :
Upvotes: 1
Reputation: 881
int count[] = new int[10];
do{
counter[pickedNum]++;
} while(pickedNum != -1);
And then use counter[0], counter[1] etc in your print statement.
Upvotes: 5