首页 > 解决方案 > 从文件中读取时尝试以另一种方法从数组中获取最大值

问题描述

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

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;
}
}

标签: javaarrayslistfilearraylist

解决方案


您的 ScoreApp 课程中已经有一个List<Student> students。在您的“<code>readStudentData”方法中,您应该将数据填充/插入到该列表中,而在您的“<code>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;
}

推荐阅读