Java - 循环和特定字符

问题描述 投票:1回答:1

我需要让程序循环直到文件结束。每次循环运行时,都应该捕获学生的姓名和考试成绩。程序应该知道它达到学生分数结束的方式是它找到“-1”。然后它应该找到他们的平均值和字母等级。最后,程序应打印出一个包含整个类摘要的文件。

我已经让它为第一个学生工作,但现在我需要让它工作到文件结束。我知道这可能不是最有效的方法,但这是我能想到的唯一方法。

public static void main(String[] args) throws FileNotFoundException {        
    //Declare String Variables
    String name = null;
    String grade;
    String inFile;
    String outFile;
    PrintWriter outputFile;

    //Declare Numerical Variables
    double score1 = 0;
    double score2 = 0;
    double score3 = 0;
    double score4 = 0;
    double score5 = 0;
    double score6 = 0;
    double score7 = 0;
    double score8 = 0;
    double count = 0;
    double average;


    //Capture Student Info
    while(scanFile.hasNext()){
        name = scanFile.nextLine();
        if (scanFile.hasNextInt()){
            score1 = scanFile.nextInt();
                count++;
            if (score1 == -1){

            }
                break;
            else if (score1 != -1)
                score2 = scanFile.nextInt();
                count++;
            if (score2 == -1){
                count--;
                break;
            }
            else if (score2 != -1)
                score3 = scanFile.nextInt();
                count++;
            if (score3 == -1){
                count--;
                break;
            }
            else if (score3 != -1)
                score4 = scanFile.nextInt();
                count++;
            if (score4 == -1){
                count--;
                break;
            }
            else if (score4 != -1)
                score5 = scanFile.nextInt();
                count++;
            if (score5 == -1){
                count--;
                break;
            }
            else if (score5 != -1)
                score6 = scanFile.nextInt();
                count++;
            if (score6 == -1){
                count--;
                break;
            }
            else if (score6 != -1)
                score7 = scanFile.nextInt();
                count++;
            if (score7 == -1){
                count--;
                break;
            }
            else if (score7 != -1)
                score8 = scanFile.nextInt();
                count++;
        }
    }
            //Processed data for student
            average = findAverage(score1, score2, score3, score4, score5, score6, score7, score8, count);
            grade = letterGrade(average);

            //Printing Output File
                      outputFile.print("Grade Report for Introduction to Programming I\n\n");
                      outputFile.print("This program will process test scores.");
                    //Output
                printFile(outputFile, name, count, average, grade);
                //}
            outputFile.close();
        }

示例输入文件

 John Sweet
 87 76 90 100 -1
 Ali Hassan
 -1
 Willy Nilly
 73 63 74 70 -1
 Juju Smith Jr.
 89 90 78 88 -1
 Karl Kavington III
 90 100 80 70 -1
 Lary Howard Holiday
 80 77 67 67 -1
 Leo Gordon
 56 88 780 77 -1
 Jessy Brown
 -1
 Mr. Perfect
 100 100 100 100 -1
 Mr. Missing It All
 0 0 0 0 0 0 -1
java loops java.util.scanner
1个回答
0
投票

我猜你的目标是获得每个学生的平均成绩,在这种情况下,代码将是这样的(而不是你的“手动循环”方法):

while(scanFile.hasNext()){ //get average of each student
    name = scanFile.nextLine();
    double count = 0, totalScore = 0;
    while (scanFile.hasNextInt()){
        totalScore = scanFile.nextInt();
        count++;
    }
    double average = findAverage(totalScore, count);
    //Processed data for student
    grade = letterGrade(average);
}

注意我已经改变了你调用findAverage方法的方法,在你的代码中更改它应该相当简单。获得平均值后,做任何你需要做的事情。

我认为你仍然遇到Java中的基本循环问题,我建议阅读更多关于如何使用它们,这里是一个recommendation

© www.soinside.com 2019 - 2024. All rights reserved.