尝试在读取文件时从另一种方法中的数组中获取最大值



我有一个包含以下数据的文件(学生证、分数(

L0111,80.5
L0222,70.8
L0333,95.4
L0444,67.9
L0555,56.5

我试图完成的是使用 ScoreApp 类中的"readStudentData"方法读取它,但以当前在该方法中打印的方式打印,但通过 printAll(( 方法完成。问题是我无法访问"readStudentData"中的"items"数组。除此之外,我无法通过"getMaxScore"方法获得最高分。

我的代码:

import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class question5
{
public static void main(String[] args) throws IOException
{
ScoreApp app = new ScoreApp("C:/Users/USER/workspace/Practical 2/Data/data");
app.printAll();
out.println(String.format("%50s", "").replace(' ', '-'));
out.println("Max score: " + app.getMaxScore());
out.println("Average score: " + app.getAverageScore());
out.println("Number of pass scores: " + app.countPassScore());
out.println("Total score (recursion): " +
recursiveGetTotalScore(app.getStudents()));
}
static double recursiveGetTotalScore(List<Student> list)
{
return 0;
}
}
class ScoreApp
{
public static List<Student> students;
public List<Student> getStudents() //getter method
{
return students;
}
public ScoreApp(String data) throws IOException
{
students = new ArrayList<>();
readStudentData(data);
}
private void readStudentData(String data) throws IOException
{
students = new LinkedList<Student>();
Path path = new File(data).toPath();
List<String> content = Files.readAllLines(path);
for(String line : content){
String[] items = line.split(",");
String id = items[0];
double score = Double.valueOf(items[1]);
Student b = new Student(id, score);
out.println(items[0]+": "+items[1]);
}
}
public void printAll()
{
}
public double getMaxScore()
{
double MaxScore = items[1];
for(int i=1;i < items[1].length; i++)
if(items[i] > MaxScore)
MaxScore = items[i];
out.println("Maximum score: " + MaxScore);
}
public double getAverageScore()
{
return 0;
}
public int countPassScore()
{
return 0;
}
}
class Student
{
private String ID;
private double score;
public Student(String ID, double score)
{
this.ID = ID;
this.score = score;
}
public String getID()
{
return this.ID;
}
public void setID(String ID)
{
this.ID = ID;
}
public double getscore()
{
return this.score;
}
public void setscore(double score)
{
this.score = score;
}
}

ScoreApp 类中已有List<Student> students。在你的"readStudentData"方法中,你应该在那个列表中填充/插入数据,在你的"getMaxScore()"方法中,你应该使用该列表。

public double getMaxScore() {
double maxScore = 0;
//"students" is the class variable / list you have in your ScoreApp class
for(Student current: students) {
double currentScore = current.getScore();
if(currentScore > maxScore)
maxScore = currentScore;
}
return maxScore;
}

更新:平均分。

public double getAverageScore() {
double averageScore = 0;
int noOfStudents = 0;
int sumOfScores = 0;
for(Student current: students) {
double currentScore = current.getScore();
sumOfScores += currentScore;
noOfStudents++;
averageScore = sumOfScores / noOfStudents;
}
return averageScore;
}

最新更新