Java - 循环和特定字符

标签 java loops java.util.scanner

我需要让程序循环直到文件末尾。每次循环运行时,它都应该捕获学生的姓名和测试分数。当程序发现“-1”时,程序应该知道它已达到学生分数的末尾。然后应该找到他们的平均分和字母等级。最后,程序应该打印出一个文件,其中包含整个类(class)的摘要。

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

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

最佳答案

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

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 .

关于Java - 循环和特定字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55755932/

相关文章:

java - 未显示货架选项卡时如何恢复 Intellij 中搁置的更改?

python - 综合列表中的两个for循环

java - 文件与程序位于同一文件夹中时出现 FileNotFoundException

java - 使用 int 变量控制 Do...While() 循环不起作用

java - Oracle 11g 中的 Hibernate 和 DOUBLE

java - Spring security : User class without UserRole (roles). 仅认证不需要授权

java - 使用循环创建对象的 ArrayList,检查重叠对象

ruby-on-rails - 如何在 Rails View 中循环并打印多维数组?

Java - 限制扫描仪仅接受 double

java - 如何在 Spring Boot 属性中生成当前年份