从学生列表中查找最低和最高成绩

Finding minimum and maximum grade from a List of students

我必须从 List 个学生中找到最低和最高一年级,然后将这些值写入只有名字的输出文件。

我试图将这个问题描绘成一个值矩阵,其中每一行对应一个学生,每一列对应他们的字段(姓名、姓氏等)。我试图将学生读入 ArrayList,然后使用 for 循环将他们的浮动成绩从 Arraylist 转移到数组中,但它不起作用。

有人知道怎么做吗?

public class student implements Comparable<student>{
       
private String fname;
private String lname;
private String major;
private float grades1;    
private float grades2; 
private float grades3;   

public String getfname() {return fname;}
public String getlname() {return lname;}
private String getmajor() {return major;}
public float getg1() {return grades1;}
public float getg2() {return grades2;}
public float getg3() {return grades3;}

public student(String f,String l,String m,float g1,float g2, float g3)  {  
    fname=f;
    lname=l;
    major=m;
    grades1=g1;
    grades2=g2;
    grades3=g3;
     
}
}
 public static void main (String[]args) throws IOException
  {
      
    PrintWriter pw1 = new PrintWriter ("input.txt");
      pw1.println ("Mickey Mouse CS 98.7 67.8 23.5");
      pw1.println ("Minnie Mouse ENG 45.6 98.3 94.7");
      pw1.println ("Donald Duck NET 56.8 74.2 78.4");
      pw1.println ("Bob Builder CS 78.5 89.4 82.5");
      pw1.println ("Snow White MAT 56.6 32.4 56.6");
      pw1.println ("Hellen Keller CHEM 78.8 23.1 99.6");
      pw1.println ("Daffy Duck ENG 67.4 55.5 89.5");
      pw1.println ("Fred Flinstone MAT 45.3 87.4 38.9");
      pw1.println ("Daffy Duck CS 76.5 22.2 88.5");
      pw1.println ("Bugs Bunny NET 68.4 89.7 95.6");
      pw1.println ("Winnie Pooh CHEM 77.5 89.4 98.2");
      pw1.close ();
    PrintWriter pw2 = new PrintWriter ("input2.txt");
      pw2.println ("Scrooge McDuck ACC 78.7 77.3 63.5");
      pw2.println ("Woody Woodbecker CS 65.6 78.3 84.7");
      pw2.println ("Scooby Doo MAT 56.8 78.2 88.4");
      pw2.println ("Spider Man CHEM 58.5 99.3 92.5");
      pw2.println ("Roger rabbit NET 66.9 39.6 86.6");
      pw2.println ("Wonder Woman CHEM 68.4 83.1 69.6");
      pw2.println ("Jane Jetson ENG 77.4 85.5 69.5");
      pw2.close ();

      File in = new File ("input.txt");
        java.util.ArrayList < student > list = uploadStudents (in);


         float[] gg = new float[list.size()];
         for (int i = 0; i < list.size(); i++)
            gg[i] = he.getg1();

float min = gg[0];
    for (int i = 0; i < gg.length; i++) {
        if (gg[i] < gg[0]) {
            gg[0] = gg[i];
        }
    }  

}
 public static ArrayList < student > uploadStudents (File inputfilename) throws IOException ///
  {
    java.util.ArrayList < student > student = new java.util.ArrayList <> ();



    Scanner sc = new Scanner (inputfilename);

    while (sc.hasNext ())
      {
    //new student object??
    student s =
      new student (sc.next (), sc.next (), sc.next (), sc.nextFloat (),
               sc.nextFloat (), sc.nextFloat ());
      student.add (s);
      }



    return student;

  } 



//I want a float[] gg array to be the one that starts with 78.88

我尝试使用 for 循环,但显然它只是将所有值复制到 gg[0];这不好,因为我还必须找到 ArrayList` 每一列的最小值,然后将它们打印到一个文件中,其中包含每个等级的最小值和最大值及其名称。

如果我对你的情况的理解正确,你试图将你的学生列表表示为值矩阵,其中每一行代表一个学生,每一列代表他们的一个领域。

您无需将学生的成绩映射到浮点数数组即可找到最低和最高成绩。您已经可以通过已有的学生名单实现这一目标。

您可以使用基本的 for 循环简单地迭代您的列表,从第二个元素开始,并将第一个学生保留为同时具有最小值和最大值的学生。在每次迭代中,您只需检查您的最小和最大学生的成绩是否分别高于和低于您当前为该字段迭代的学生 grades1

编辑:如果您想在三个成绩中找到成绩最低和最高的学生,那么您可以在 Student class 中定义方法 getMinimumGradegetMaximumGrade 分别 return 三者中最低和最高的成绩。

学生

class Student {

    //your implemenyation

    //Method to get the lowest grade among the three
    public float getMinimumGrade() {
        return Math.min(Math.min(grades1, grades2), grades3);
    }

    //Method to get the greatest grade among the three
    public float getMaximumGrade() {
        return Math.max(Math.max(grades1, grades2), grades3);
    }
}

部分主要实现

File in = new File("input.txt");
ArrayList<Student> listStudents = uploadStudents(in);

Student studentMinGrade = null;
Student studentMaxGrade = null;

if (listStudents.size() > 0) {
    //Assuming that the first student corresponds to the student with both minimum and maximum grade
    studentMinGrade = listStudents.get(0);
    studentMaxGrade = listStudents.get(0);

    for (int i = 1; i < listStudents.size(); i++) {

        //Checking if the current student has a grade lower than the minimum student
        if (listStudents.get(i).getMinimumGrade() < studentMinGrade.getMinimumGrade()) {
            studentMinGrade = listStudents.get(i);
        }

        //Checking if the current student has a grade higher than the maximum student
        if (listStudents.get(i).getMaximumGrade() > studentMaxGrade.getMaximumGrade()) {
            studentMaxGrade = listStudents.get(i);
        }
    }
}

PrintWriter pw = new PrintWriter("output.txt");

//Printing the student with the minimum grade
if (studentMinGrade != null) {
    pw.print(String.format("Student with minimum grade: %s => %f%n", studentMinGrade.getfname(), studentMinGrade.getMinimumGrade()));
}

//Printing the student with the maximum grade
if (studentMaxGrade != null) {
    pw.print(String.format("Student with maximum grade: %s => %f", studentMaxGrade.getfname(), studentMaxGrade.getMaximumGrade()));
}

pw.close();

相反,如果您正在寻找更复杂和紧凑的东西,您也可以使用流来实现。

流实现的部分主要内容

File in = new File("input.txt");
ArrayList<Student> listStudents = uploadStudents(in);

Student studentMinGrade = listStudents.stream()
        .min(Comparator.comparingDouble(Student::getMinimumGrade)) //Finding the student with the lowest grade
        .orElse(null); //Retrieving the student or null if non has been found

Student studentMaxGrade = listStudents.stream()
        .max(Comparator.comparingDouble(Student::getMaximumGrade)) //Finding the student with the greatest grade
        .orElse(null); //Retrieving the student or null if non has been found

PrintWriter pw = new PrintWriter("output.txt");

//Printing the student with the minimum grade
if (studentMinGrade != null) {
    pw.print(String.format("Student with minimum grade: %s => %f%n", studentMinGrade.getfname(), studentMinGrade.getMinimumGrade()));
}

//Printing the student with the maximum grade
if (studentMaxGrade != null) {
    pw.print(String.format("Student with maximum grade: %s => %f", studentMaxGrade.getfname(), studentMaxGrade.getMaximumGrade()));
}

pw.close();