0

私は、infileから各整数を読み取り、それをメソッドadScoreに渡そうとしています。このメソッドは、文字の成績、すべての成績の合計数、および最高の試験スコアと最低の試験スコアを決定します。しかし、forループを実行するときのwhileループは、system.out.printを実行するforループの後にデバッグしているため、infileからデータを取得していません。そして、返されるのは、ループ内の私のカウンターである0〜29の数字だけです。インファイルから成績スコアを取得できるように、私が間違っていることについて私を支援できますか?

よろしく。

        while(infile.hasNextInt())
        {
            for(int i=0; i <30; i++)//<-- it keeps looping and not pulling the integers from the     file.
            {
                System.out.println(""+i);//<--- I placed this here to test what it     is pulling in and it is just counting
                //the numbers 0-29 and printing them out.  How do I get each data    from the infile to store
                exam.adScore(i);//determines the count of A, B, C, D, F grades, total   count, min and max
            }
        }
4

2 に答える 2

2

0〜29を出力します。これは、次のように指示しているためです。

System.out.println(""+i)

ループカウンターとして使用している整数であるiを出力します。Scannerオブジェクトから実際に次の値を取得することはありません。これは宿題だと思うので、コードは提供しませんが、入力ファイルから値を取得してその値を内部で使用するには、ScannerのnextInt()メソッドを使用する必要があることは間違いありません。 forループ。

于 2012-04-15T02:04:25.123 に答える
1

トロンの言うとおり --- 実際にはスキャナに次の整数を読み取るように要求していません。 Scanner.hasNextInt()読み取る整数があるかどうかをテストするだけです。i値 0 ~ 29 をループするように指示しているだけです。私はあなたがこのようなことを意味していると思います:

while(infile.hasNextInt())
{
    int i = infile.nextInt();
    exam.adScore(i);//determines the count of A, B, C, D, F grades, total count, min and max
}

入力のすべての行が整数であるかどうかわからない場合は、次のようにすることができます。

while(infile.hasNext()) { // see if there's data available
    if (infile.hasNextInt()) { // see if the next token is an int
        int i = infile.nextInt(); // if so, get the int
        exam.adScore(i);//determines the count of A, B, C, D, F grades, total count, min and max
    } else {
        infile.next(); // if not an int, read and ignore the next token
    }
}
于 2012-04-15T05:03:26.857 に答える