计数数组引用输入并显示计数——无限循环

Counting array reference input and displaying count -- infinite loop

我试图显示一个整数在数组中出现的次数,但出现无限 loop/logic 错误。例如,如果用户输入:2, 5, 6, 5, 4, 3, 23, 43, 2, 0 那么它应该显示:

2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time

任何帮助将不胜感激。 注意:这不是作业或家庭作业,而是 Y.D Java 书的介绍中的练习题。朗

import java.util.*;

public class CountNumbers {

    public static void main(String[] args) {
        System.out.println("Enter the integers between 1 and 100: ");
        int[] arrayRefVar = createList();
        int[] countNum = countNumbers(arrayRefVar);
        displayCount(countNum, arrayRefVar);
    }

    public static int[] createList() {
        Scanner Input = new Scanner(System.in);
        int[] List = new int[100];
        int i = 0;

        while (List[i] != 0) {
            List[i] = Input.nextInt();
            i++;
        }
        return List;
    }

    public static int[] countNumbers(int[] List) {
        int[] count = new int[100];

        for (int i = 0; i < count.length; i++) {
            count[i] = i;
        }

        int[] countNum = new int[List.length];

        for (int i = 0; i < countNum.length; i++) {
            countNum[i] = 0;
        }

        for (int i = 0; i < List.length; i++) {
            for (int j = 0; j < count.length; j++) {
                if (List[i] == count[j]) {
                    countNum[i]++;
                }
            }
        }
        return countNum;
    }

    public static void displayCount(int[] countList, int[] arrayRefVar) {
        for (int i = 0; i < arrayRefVar.length; i++) {
            System.out.println(arrayRefVar[i] + " occurs " + countList[i] + " " + checkPlural(arrayRefVar[i]));
        }
    }

    public static String checkPlural(int n) {
        if (n > 1) {
            return "times";
        } else {
            return "time";
        }
    }
}

一个问题是从未输入用户输入的 while 循环。您使用 0 作为标记值来退出用户输入,但是,当您初始化一个整数数组时,默认情况下它们都是 0。

int[] List = new int[100];

int i = 0;
//problem: while loop never entered
while (List[i] != 0) {

如果你的输入应该以0结尾,那么你应该检查当前读取的int是否为零。

while(i < List.length) {
    List[i] = Input.nextInt();
    if(List[i] == 0)
        break ;
    ++i;
}

由于您在递增 i 后检查条件,因此您没有检查当前值。

注意:Scanner class 中的 nextInt() 方法可以抛出异常,即:InputMismatchExceptionNoSuchElementExceptionIllegalStateException。因此,要么在 try catch 块中处理它,要么让调用者通过抛出异常来处理它。

经过无数次尝试,我终于明白了,如果有人有任何建议可以提高代码效率,我们将不胜感激。这是:

导入java.util.Random;

public class CountSingleDigits {

public static void main (String[] args) {

    int[] arrayRefVar = createList();

    int[] counts = new int[10];

    for (int i = 0; i < 10; i++) {

        counts[i] = i;
    }

    int[] tempCounts = new int[10];

    for (int i = 0; i < arrayRefVar.length; i++) {

        for (int j = 0; j < 10; j++) {

            if (arrayRefVar[i] == counts[j]) {

                tempCounts[j]++;    
            }
        }
    }

    for (int i = 0; i < 10; i++) {

        System.out.println(counts[i] + " appears " + tempCounts[i] + " times ");
    }

    for (int i = 0; i < arrayRefVar.length; i++) {

        if (i % 10 == 0) {

            System.out.println();
        }

        System.out.print(arrayRefVar[i] + " ");
    }
}

public static int[] createList() {

    Random f = new Random();

    int[] List = new int[100];

    for (int i = 0; i < List.length; i++) {

        List[i] = f.nextInt(9);
    }

    return List;
}

}