使用混合了整数和字符串的.txt输入文件计算平均值,最大值和最小值

时间:2014-12-05 21:36:02

标签: java io max average min

请帮忙。我到处寻找并且很难知道如何完成这个程序。我需要从数据输入文件中计算平均值,最大值和最小值。他们需要看起来像这样:

System.out.println("The average total score of the class is: " + total/27);
System.out.println(maxName + "got the maximum score of: " + maxVal);
System.out.println(minName + "got the maximum score of: " + minVal);

对于平均值,我不知道为什么我的main中的while循环不起作用。它计算零值。我也不知道我怎么能这样做,所以27不是硬编码。对于最大值和最小值,我对这些没有任何线索。我甚至没有猜测。我已经看过很多关于这些使用方法的东西我们没有被教过(例如缓冲的东西,或者使用我们还没有学过的Collections类。我只需要一种基本的完成方式到目前为止,我将包括我所拥有的一切。感谢任何帮助。谢谢。

输入文件:

9
Andy Borders
200
250
400
John Smith
120
220
330
Alvin Smith
225
300
278
Mike Borell
250
250
500
Jim Jones
325
325
155
Robert Fennel
200
150
350
Craig Fenner
230
220
480
Bill Johnson
120
150
220
Brent Garland
220
240
350

主:

import java.util.Scanner;
import java.io.*;
public class Project5 {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the file name: ");
    String fileName = in.nextLine();
    try {
        File file = new File(fileName);
        Scanner inputFile = new Scanner(file);
        String SnumStudents = inputFile.nextLine();
        int numStudents = Integer.parseInt(SnumStudents);
        Student [] studentList = new Student[numStudents];
        for (int i = 0; i < numStudents; i++)
        {
            String line = inputFile.nextLine();
            String score1 = inputFile.nextLine();
            String score2 = inputFile.nextLine();
            String score3 = inputFile.nextLine();
            studentList [i] = new Student(line, Integer.parseInt(score1), Integer.parseInt(score2), Integer.parseInt(score3));
        }
        System.out.println("Name\t\tScore1\tScore2\tScore3\tTotal");
        System.out.println("---------------------------------------------");
        for (int i=0; i< studentList.length;i++){
            System.out.println(studentList[i].getName() + "\t" + studentList[i].getScore1() + "\t" + studentList[i].getScore2() + "\t" + studentList[i].getScore3() + "\t" + studentList[i].getTotal());
        }
        System.out.println("---------------------------------------------");
        System.out.println("The total number of students in this class is: " + studentList.length);
        int total = 0;
        while (inputFile.hasNextInt())
        {
            int value = inputFile.nextInt();
            total = total + value;
        }
        System.out.println("The average total score of the class is: " + total/27);
        System.out.println(maxName + "got the maximum score of: " + maxVal);
        System.out.println(minName + "got the maximum score of: " + minVal);
        inputFile.close();
    } catch (IOException e) {
        System.out.println("There was a problem reading from " + fileName);
    }
    finally {
    }
    in.close();
}
}

学生班:

public class Student {
private String name;
private int score1;
private int score2;
private int score3;
private int total;
private double average;

public Student(String n, int s1, int s2, int s3){
    name = n;
    score1 = s1;
    score2 = s2;
    score3 = s3;
    total = s1 + s2 + s3;
}
public String getName(){
    return name;
}
public int getScore1(){
    return score1;
}
public int getScore2(){
    return score2;
}
public int getScore3(){
    return score3;
}
public int getTotal(){
    return total;
}
public double computeAverage(){
    return average;
}
}

1 个答案:

答案 0 :(得分:0)

你的while循环永远不会被执行。当您在for循环中读完文件的行时,不再需要使用该文件的行。所以,inputFile.hasNextInt()永远不会成真。

由于行

,27是硬编码的
System.out.println("The average total score of the class is: " + total/27);

您需要将此处的27更改为一个代表总分数的变量。

您正在存储每个人的所有分数。要找到min(max),您只需要遍历每个值并找到最小(最大)值并存储它们。