[英]Counting how many times each number was typed
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. 我试图计算循环中键入0到9之间的每个数字的次数,按-1会停止循环,我可以长期使用它,而且我还必须打印键入的数字最多。 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. 编辑:忘记提及仅使用基础知识,而不使用高级方法,数组和循环都很好。
int count[] = new int[10];
do{
counter[pickedNum]++;
} while(pickedNum != -1);
And then use counter[0], counter[1] etc in your print statement. 然后在您的打印语句中使用counter [0],counter [1]等。
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 : 并输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.