0

为什么我在编译代码时会收到无法访问的语句错误。错误出现在代码的最后一行 ( System.out)。它在 main 方法中,我不明白为什么它无法访问。

public class Count {

    public static void main(String[] args) {
        Vector numberList = new Vector();
        double randomNum;

        //for loop to get numbers and add to vector
        for (int i = 0; i <= 9999; i++) {
            do {
                randomNum = Math.random();
            } while (randomNum < .01 || randomNum > .99);

            //takes the random number, rounds it, and multiplies it by 100
            //so that the numbers go from 1 to 99
            Math.round(randomNum *= 100);
            //converts the double to an int
            int tempNum = (int) randomNum;
            //the vector is built
            numberList.add(i, tempNum);
        }

        //sorts numbers
        Collections.sort(numberList);

        int count[] = new int[99];

        for (int j = 1;; j++) {
            for (int i = 0; i < 9999; i++) {
                if ((numberList.elementAt(i)) == j) {
                    count[j] += 1;
                }
            }
        }

        System.out.println(count[1]);
    }
}
4

3 回答 3

3

您的外部for循环没有结束条件:

for(int j=1;;j++){

j将无限增加。您可能正在寻找:

for (int j = 0; j < count.length; j++) {
于 2013-01-26T00:35:02.463 回答
1

你的 for 循环中没有条件 for(int j=1;;j++){

更像 for(int j=1;j<1000;j++){ 修复它?

于 2013-01-26T00:35:29.280 回答
0
if ((numberList.elementAt(i)) == j)

不兼容的操作数类型 Object 和 int。

似乎上面的代码永远不会被执行。

于 2013-01-26T00:40:29.277 回答