从Text中读取整数并对其进行总结

时间:2013-06-07 14:56:05

标签: java arraylist integer max min

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

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

我的问题是:我不知道如何使用变量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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

不太确定应用程序的逻辑,特别是do循环是否应该在while循环内,但我建议你使用括号,即使控制结构只有一个语句。您将更好地概述您的意图,并减少难以找到的错误的可能性。