0

你将编写一个程序来计算考试成绩列表的统计数据,即考试统计程序 (ESP)。输入是一个文本文件的名称,其中包含分数的数量,后跟分数列表。(请参见下面的示例。)然后您应该显示以下信息:

一个。分数的数量
B. 最低、最高和平均分数
C. As、Bs、Cs、Ds 和 Fs 的数量,使用 90-80-70-60 的比例。

我的问题是:我不知道如何获得可变grade数字。

当我运行这个程序时,它给了我这个错误:

Exception in thread "main" java.util.NoSuchElementException
     at java.util.Scanner.throwFor(Unknown Source)
     at java.util.Scanner.next(Unknown Source)
     at java.util.Scanner.nextInt(Unknown Source)
     at java.util.Scanner.nextInt(Unknown Source)
     at IntegersFromFile.main(IntegersFromFile.java:60)

这是我的代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class IntegersFromFile 
{
    public static void main(String[] args) 
        {
        Scanner file = null;

        int grade = 0;
        int min = 100;
        int max = 0;
        ArrayList<Integer> grades = new ArrayList<Integer>();

        int a, b, c, d, f;
        a = 0;
        b = 0;
        c = 0;
        d = 0;
        f = 0;

        double aA, aB, aC, aD, aF;

        aA = 0;
        aB = 0;
        aC = 0;
        aD = 0;
        aF = 0;

        System.out.println("***Welcome to the Exam Statistics Program!!***");
        System.out.println("Please enter the name of your data file:");

        Scanner input = new Scanner(System.in);
        String file_name = input.nextLine();

        try 
            {
            file = new Scanner(new File(file_name));
            }

        catch (FileNotFoundException e) 
            {
            e.printStackTrace();
            }

        while (file.hasNext()) 
            {
            if (file.hasNextInt()) 
                {
                grades.add(file.nextInt());
                } 
            else
                file.next();
            }

        do 
            {
            grade = file.nextInt();

            if (grade >= 0) 
                {
                grades.add(grade);

                if (grade < 60) 
                    {
                    f++;
                    aF += grade;
                    } 
                else if (grade < 70) 
                    {
                    d++;
                    aD += grade;
                    } 
                else if (grade < 80) 
                    {
                    c++;
                    aC += grade;
                    } 
                else if (grade < 90) 
                    {
                    b++;
                    aB += grade;
                    } 
                else 
                    {
                    a++;
                    aA += grade;
                    }
                if (grade < min)
                    min = grade;
                if (grade > max)
                    max = grade;
                }
            } 
        while (grade >= 0);

        if (aA > 0)
            aA /= a;
        if (aB > 0)
            aB /= b;
        if (aC > 0)
            aC /= c;
        if (aD > 0)
            aD /= d;
        if (aF > 0)
            aF /= f;

        double avg = 0;
        for (int i = 0; i < grades.size(); i++) {
            avg += grades.get(i);
            }   

        avg /= grades.size();

        System.out.println("Total Number of Scores: " + grades.size());
        System.out.println("Total As: " + a);
        System.out.println("Total Bs: " + b);
        System.out.println("Total Cs: " + c);
        System.out.println("Total Ds: " + d);
        System.out.println("Total Fs: " + f);

        System.out.println("Average A: " + aA);
        System.out.println("Average B: " + aB);
        System.out.println("Average C: " + aC);
        System.out.println("Average D: " + aD);
        System.out.println("Average F: " + aF);
        System.out.println("The highest grade was " + max);
        System.out.println("The lowest grade was " + min);
        System.out.println("The average grade was " + avg);
    }
}
4

1 回答 1

0

问题是当程序执行到第 60 行时,扫描仪的输入“已用尽”。您应该在开始执行 do-cycle 之前重新初始化file变量(建议您将其重命名为或类似名称)。scanner

不太确定应用程序的逻辑,尤其是 do-cycle 是否应该在 while-cycle 内,但我建议您使用括号,即使控制结构只有一个语句。这样您将更好地概述您的意图,并减少难以发现的错误的可能性。

于 2013-06-10T12:20:01.523 回答