计算每个数字的输入次数

时间:2014-11-27 17:08:55

标签: java

我试图计算在循环中键入0到9之间的每个数字的次数,当按-1时它将停止循环,我使其长期工作,而且我将不得不打印其中的哪一个数字输入最多。 现在我只有couting代码:

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);

正如你所看到的,它工作正常,但我知道有更好的选择来做这件事。 正如我所提到的,我还需要打印最大数量的数字(这个数字类型最多),并且用我的编程方式来达到这个目标还有很长的路要走。 我希望得到任何提示或注意事项,以便更轻松,更快地完成工作。 任何笔记都会受到影响。

编辑: 忘了提到只使用基础知识,而不是高级方法,数组和循环都没问题。

2 个答案:

答案 0 :(得分:5)

int count[] = new int[10];
do{
    counter[pickedNum]++;
} while(pickedNum != -1);

然后在print语句中使用counter [0],counter [1]等。

答案 1 :(得分:1)

不要忘记你可以拥有多个输入次数最多的号码,请看一下:

 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 + ", ");
        }

    }
}

输出:

enter image description here