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

时间:2015-02-11 21:44:12

标签: java infinite-loop

我试图显示一个整数在数组中出现的次数,但是我得到一个无限循环/逻辑错误。例如,如果用户输入: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";
        }
    }
}

3 个答案:

答案 0 :(得分:0)

如果您的输入应以0结尾,则应检查当前读取的int是否为零。

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

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

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

答案 1 :(得分:0)

所以我经过无数次尝试后终于得到了它,如果有人有任何建议使代码更有效,那么输入将非常受欢迎。这是:

import java.util.Random;

公共类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;
}

}

答案 2 :(得分:-1)

一个问题是永远不会输入用户输入的while循环。使用0作为sentinel值来退出用户输入,但是,在初始化整数数组时,默认情况下它们都是0。

int[] List = new int[100];

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