【发布时间】:2015-02-04 03:56:59
【问题描述】:
请帮忙。我到处看了看,不知道如何完成这个程序。我需要从数据输入文件中计算平均值、最大值和最小值。他们需要看起来像这样:
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;
}
}
【问题讨论】: