【问题标题】:Need help sorting ArrayList & Calculating averages of test scores需要帮助排序 ArrayList 和计算测试分数的平均值
【发布时间】:2020-08-25 05:48:35
【问题描述】:

我尝试根据给出的建议添加一个新类,见第 67 行。我不确定如何将新类与从用户输入创建的条目链接起来,目标是按以下方式对 ArrayList 进行排序姓氏并计算每个条目的平均值 4 个测试分数,得出一个平均分数 - 我希望将平均分数添加到每个学生的条目并添加到最终的 ArrayList

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
 

//information given by user input that will eventually go into the ArrayList 
public class studentInformation {
        

    public static <T> void main(String[] args) {
        
        //creating ArrayList to hold the objects created above
        ArrayList<Object> studentdatabase = new ArrayList<Object>();
        
        char cont;
        do {
            Scanner fnInput = new Scanner(System.in);
            System.out.println("Enter Student's First Name & Press Enter");
            //String fn = fnInput.nextLine();
            studentdatabase.add(fnInput.nextLine());
        
            Scanner lnInput = new Scanner(System.in);
            System.out.println("Enter Student's Last Name & Press Enter");
            //String ln = lnInput.nextLine();
            studentdatabase.add(lnInput.nextLine());
        
            Scanner score1Input = new Scanner(System.in);
            System.out.println("Enter Student's First Exam Score & Press Enter");
            //int score1 = score1Input.nextInt();
            studentdatabase.add(score1Input.nextInt());
        
            Scanner score2Input = new Scanner(System.in);
            System.out.println("Enter Student's Second Exam Score & Press Enter");
            //int score2 = score2Input.nextInt();
            studentdatabase.add(score2Input.nextInt());
        
            Scanner score3Input = new Scanner(System.in);
            System.out.println("Enter Student's Third Exam Score & Press Enter");
            //int score3 = score3Input.nextInt();
            studentdatabase.add(score3Input.nextInt());
            
            
            Scanner score4Input = new Scanner(System.in);
            System.out.println("Enter Student's Fourth/Final Exam Score & Press Enter");
            //int score4 = score4Input.nextInt();
            studentdatabase.add(score4Input.nextInt());
            
            
            Scanner continueInput = new Scanner(System.in);
            System.out.println("Enter 'C' to end or 'A' to Add More");
            cont = continueInput.next().charAt(0);
            
            //calculate the average score for each student 
            
            
            //sort the ArrayList prior to printing
            //Collections.sort(studentdatabase);
            
            //Prints out the arrayList
            System.out.println(studentdatabase);
        }
        while(cont != 'c' || cont != 'C');
        
    }
    
    class Students {
        String firstName, lastName;
        int firstScore, secondScore, thirdScore, fourthScore, averagescore;
        char lettergrade;
        
    }
    
}

【问题讨论】:

  • 1) 不要在System.in 上创建多个Scanner。 --- 2) 不要将信息添加到ArrayList。 --- 3) 将班级Students 重命名为Student,因为班级的一个实例仅代表一名学生。 --- 4) 使用new 运算符创建Student 的实例。请参阅您的 Java 学习指南了解如何操作。 --- 5) 将用户输入的值分配给您创建的Student 实例的字段。 --- 6) ...好吧,让我们从那里开始,看看你能走多远...

标签: java sorting arraylist


【解决方案1】:

首先,创建一个真正的 Student 类(单数,而不是复数)。这个类的一个实例只有一个学生。它将以专用方法处理平均值计算。

考虑在属性上使用带有正确访问器的 getter 和 setter。

public class Student {

    private String firstName, lastName;
    private int firstScore, secondScore, thirdScore, fourthScore, averagescore;
    private char lettergrade;

    public float computeAverage(){

        int sum = firstScore + secondScore + thirdScore + fourthScore;

        return (float) sum / 4;

    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getFirstScore() {
        return firstScore;
    }

    public void setFirstScore(int firstScore) {
        this.firstScore = firstScore;
    }

    public int getSecondScore() {
        return secondScore;
    }

    public void setSecondScore(int secondScore) {
        this.secondScore = secondScore;
    }

    public int getThirdScore() {
        return thirdScore;
    }

    public void setThirdScore(int thirdScore) {
        this.thirdScore = thirdScore;
    }

    public int getFourthScore() {
        return fourthScore;
    }

    public void setFourthScore(int fourthScore) {
        this.fourthScore = fourthScore;
    }

    public int getAveragescore() {
        return averagescore;
    }

    public void setAveragescore(int averagescore) {
        this.averagescore = averagescore;
    }

    public char getLettergrade() {
        return lettergrade;
    }

    public void setLettergrade(char lettergrade) {
        this.lettergrade = lettergrade;
    }
}

那么,不要在列表中使用 Object 而是使用 Student。您创建了一个对象。使用它!

在完成输入列表之前,您无法对列表进行排序。将排序排除在循环之外。

cont != 'c' || cont != 'C'

永远是真的。所以你永远不会摆脱你的循环。

最后,我会建议这样的事情。

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

    public static <T> void main(String[] args) {

        //creating ArrayList to hold the objects created above
        ArrayList<Student> studentdatabase = new ArrayList<Student>();

        char cont;
        do {

            Student currentStudent = new Student();

            Scanner fnInput = new Scanner(System.in);
            System.out.println("Enter Student's First Name & Press Enter");
            currentStudent.setFirstName(fnInput.nextLine());

            Scanner lnInput = new Scanner(System.in);
            System.out.println("Enter Student's Last Name & Press Enter");
            currentStudent.setLastName(lnInput.nextLine());

            Scanner score1Input = new Scanner(System.in);
            System.out.println("Enter Student's First Exam Score & Press Enter");
            currentStudent.setFirstScore(score1Input.nextInt());

            Scanner score2Input = new Scanner(System.in);
            System.out.println("Enter Student's Second Exam Score & Press Enter");
            currentStudent.setSecondScore(score2Input.nextInt());

            Scanner score3Input = new Scanner(System.in);
            System.out.println("Enter Student's Third Exam Score & Press Enter");
            currentStudent.setThirdScore(score3Input.nextInt());

            Scanner score4Input = new Scanner(System.in);
            System.out.println("Enter Student's Fourth/Final Exam Score & Press Enter");
            currentStudent.setFourthScore(score4Input.nextInt());

            studentdatabase.add(currentStudent);

            Scanner continueInput = new Scanner(System.in);
            System.out.println("Enter 'C' to end or 'A' to Add More");
            cont = continueInput.next().charAt(0);

            //Prints out the arrayList
            System.out.println(studentdatabase);
        }
        while(cont != 'c' && cont != 'C');


        //sort the arrayList prior to printing
        studentdatabase.sort(Comparator.comparing(Student::getLastName));
        //studentdatabase.sort(Comparator.comparing(Students::getLastName).reversed());

        for (Student student:studentdatabase) {
            System.out.println(student.getLastName() + " " + student.getFirstName() + " : " + student.computeAverage());
        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2017-08-17
    相关资源
    最近更新 更多