将单独的“double”值转换为ArrayList

时间:2015-11-11 15:53:15

标签: java arrays arraylist

我创建了一个程序,其中包含一个ArrayList,其中包含学生成绩的“double”值。但是,该课程要求创建一个等级的int数组,而不是我以前的单独值。

我的学生班级代码(我删除了不相关的代码以切入追逐):

public class Student {

    private int[] Grades = new int[3];

    public Student(String stuid, String fname, String lname, String email, int age, int[] grades) {
        this.Grades = grades;
    }

    public int[] getGrades() {
        return Grades;
    }

    public void setGrades(int[] grades) {
        Grades = grades;
    }

    public String toString() {
        return String.format("StuID: %s\t First Name: %s\t Last Name: %s\t E-Mail: %s\t Age: %s\t Grades: %s\t",
                this.StuID, this.FName, this.LName, this.Email, this.Age, this.Grades);
    }
}

名册等级:

import java.util.ArrayList;

public class Roster {
    static ArrayList<Student> studentArray;

    public Roster(ArrayList<Student> ar) {
        studentArray = ar;
    }

    // 3.C - Print Average Grade
    public static void print_average_grade(String studentID) {
        for (Student v : studentArray) {
            if (v.getStuID().equals(studentID)) {
                double total = v.getGrade1() + v.getGrade2() + v.getGrade3();
                double average = total / 3;
                System.out.println("Student ID#" + studentID + " Grade AVG= " + average);
            }
        }
    }

    public static void main(String[] args) {

        ArrayList<Student> studentArray = new ArrayList<Student>();

        studentArray.add(new Student("1", "John", "Smith", "John1989@gmail.com", 20, 88, 79, 59));
        studentArray.add(new Student("2", "Susan", "Erickson", "Erickson_1990@gmailcom", 19, 91, 72, 85));
        studentArray.add(new Student("3", "Jack", "Napoli", "The_lawyer99yahoo.com", 19, 85, 84, 87));
        studentArray.add(new Student("4", "Erin", "Black", "Erin.black@comcast.net", 22, 91, 98, 82));
        studentArray.add(new Student("5", "Captain", "Planet", "PowIsYours@planet.edu", 65, 99, 98, 97));

        new Roster(studentArray);
        for (Student v : studentArray) {
            print_average_grade(v.getStuID());
        }
    }
}

我已将分隔值(Grade1,Grade2,Grade3)更改为“int [] Grades”数组并修改了构造函数并添加了setter和getter。所以,我认为学生班很好,但是名册班,是我被困的地方。两件事:

1)如何将Grades的值添加到现在属于ArrayList的Array中?

2)如何调整AVG Grade方法以执行与之前相同的任务,但是使用数组的值?

任何帮助都会很棒,因为我已经坚持了好几天。

感谢。

P.S。如果发布完整代码会更容易,我很乐意发布它以解决这个问题。

2 个答案:

答案 0 :(得分:0)

我尝试根据两个要求(数组,AVG)修复代码。请参阅以下代码(我已对其进行了测试)。

学生班:

public class Student {

    String StuID;
    String FName;
    String LName;
    String Email;
    int Age;
    private int[] Grades = new int[3];

    public Student(String stuid, String fname, String lname, String email, int age, int[] grades) {
        this.StuID = stuid;
        this.FName = fname;
        this.LName = lname;
        this.Email = email;
        this.Grades = grades;
    }

    public int[] getGrades() {
        return Grades;
    }

    public void setGrades(int[] grades) {
        Grades = grades;
    }

    public String toString() {
        return String.format("StuID: %s\t First Name: %s\t Last Name: %s\t E-Mail: %s\t Age: %s\t Grades: %s\t", this.StuID, this.FName, this.LName, this.Email, this.Age, this.Grades);
    }

    public String getFName() {
        return FName;
    }

    public void setFName(String fName) {
        FName = fName;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public int getAge() {
        return Age;
    }

    public void setAge(int age) {
        Age = age;
    }

    public String getStuID() {
        return StuID;
    }

    public void setStuID(String stuID) {
        StuID = stuID;
    }

    public String getLName() {
        return LName;
    }

    public void setLName(String lName) {
        LName = lName;
    }
}

名册等级:

import java.util.ArrayList;

public class Roster {
    ArrayList<Student> studentArray;

    public Roster(ArrayList<Student> ar) {
        studentArray = ar;
    }

    // 3.C - Print Average Grade
    public void print_average_grade(String studentID) {
        for (Student v : studentArray) {
            if (v.getStuID().equals(studentID)) {
                double total = v.getGrades()[0] + v.getGrades()[1] + v.getGrades()[2];
                double average = total / 3;
                System.out.println("Student ID#" + studentID + " Grade AVG= " + average);
            }
        }
    }

    public static void main(String[] args) {

        ArrayList<Student> studentArray = new ArrayList<Student>();

        int[] grades1 = {88, 79, 59};
        int[] grades2 = {91, 72, 85};
        int[] grades3 = {85, 84, 87};
        int[] grades4 = {91, 98, 82};
        int[] grades5 = {99, 98, 97};

        studentArray.add(new Student("1", "John", "Smith", "John1989@gmail.com", 20, grades1));
        studentArray.add(new Student("2", "Susan", "Erickson", "Erickson_1990@gmailcom", 19, grades2));
        studentArray.add(new Student("3", "Jack", "Napoli", "The_lawyer99yahoo.com", 19, grades3));
        studentArray.add(new Student("4", "Erin", "Black", "Erin.black@comcast.net", 22, grades4));
        studentArray.add(new Student("5", "Captain", "Planet", "PowIsYours@planet.edu", 65, grades5));

        Roster r = new Roster(studentArray);
        for (Student v : studentArray) {
            r.print_average_grade(v.getStuID());
        }
    }
}

输出:

Student ID#1 Grade AVG= 75.33333333333333
Student ID#2 Grade AVG= 82.66666666666667
Student ID#3 Grade AVG= 85.33333333333333
Student ID#4 Grade AVG= 90.33333333333333
Student ID#5 Grade AVG= 98.0

答案 1 :(得分:0)

这是我如何做同样的项目。我的评分仍在评分中,所以我不知道它是否会第一次通过。但也许它会通过我分享来帮助别人。

Container