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 に答える