Java 计算每个数字的输入次数

Java 计算每个数字的输入次数,java,Java,我试图计算0到9之间的每个数字在一个循环中输入了多少次,当按-1键时,它将停止循环,我使它长期工作,而且我必须打印出输入的数字最多。 现在我只有密码: 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 ente

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

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);
正如你所看到的,它工作得很好,但我知道有一种更好的方法来做这件事。 正如我所提到的,我还需要打印最大的数字计数(哪一个数字是类型最多的),用我的编程方法实现这一点需要很长的路要走。 我想得到任何提示或笔记,以改善这一工作更容易和更快。 任何注释都将被告知

编辑: 忘了提到只使用基本方法,而不是高级方法,数组和循环都可以

int count[] = new int[10];
do{
    counter[pickedNum]++;
} while(pickedNum != -1);
然后在打印语句中使用计数器[0]、计数器[1]等


然后在打印语句中使用计数器[0]、计数器[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 + ", ");
        }

    }
}
以及输出:


别忘了,您可以输入最多的多个号码,请查看以下内容:

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

    }
}
以及输出:


地图中存储条目
。我编辑我的帖子,忘了提到我只需要使用基本方法而不需要使用高级方法,数组和循环都可以。在
地图中存储条目
。我编辑帖子,忘了提到我只需要使用基本方法而不需要使用高级方法,数组和循环都可以。