Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java循环和特定字符_Java_Loops_Java.util.scanner - Fatal编程技术网

Java循环和特定字符

Java循环和特定字符,java,loops,java.util.scanner,Java,Loops,Java.util.scanner,我需要让程序循环直到文件结束。在循环的每次运行中,它都应该捕获学生的姓名和考试成绩。程序应该知道学生的分数已经到了末尾,当它找到“-1”时,它应该找到他们的平均分和字母分数。最后,程序应该打印出一个包含整个类的摘要的文件 我已经让它为第一个学生工作,但现在我需要让它工作到文件结束。我知道这可能不是最有效的方法,但这是我能想到的唯一方法 public static void main(String[] args) throws FileNotFoundException {

我需要让程序循环直到文件结束。在循环的每次运行中,它都应该捕获学生的姓名和考试成绩。程序应该知道学生的分数已经到了末尾,当它找到“-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

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

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


我认为您在Java中的基本循环方面仍然存在问题,我建议您阅读更多关于如何使用它们的内容,这里有一个。

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

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


我认为您在Java中的基本循环方面仍然存在问题,我建议您阅读更多关于如何使用它们的内容,这里有一个。

您知道接口
Java.util.List
及其实现,例如
Java.util.ArrayList
?这段代码甚至可以编译吗?我会感到惊讶。您知道接口
java.util.List
及其实现,如
java.util.ArrayList
?这些代码甚至可以编译吗?我会感到惊讶的。
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);
}