当前位置: 首页 > 知识库问答 >
问题:

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

樊烨烨
2023-03-14

我创建了一个程序,其中包含一个ArrayList,其中包含学生成绩的单独“双”值。然而,本课程要求创建一个分数整数数组,而不是我之前使用的单独值。

我的学生类代码(我已经删除了不相关的代码,开门见山):

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)如何将等级的值添加到现在是ArrayList一部分的数组中?

2) 我如何调整我的平均等级方法来执行与之前相同的任务,但使用数组的值?

任何帮助都会很好,因为我已经被困在这个问题上好几天了。

谢谢

P、 如果发布完整的代码会更容易,我将很乐意发布它,以帮助解决这个问题。

共有2个答案

南宫阳冰
2023-03-14

下面是我如何完成相同项目的。我的仍在评分中,所以我不知道它是否会第一次通过。但也许我的分享会对某人有所帮助。

import java.util.ArrayList;

public class Roster {

   public static ArrayList<Student> students = new ArrayList<>();   

    public static void main(String[] args)
    {
        // input student grades
        int[] grade0 = {0,0,0}; // filler to make grade array equal to student ID
        int[] grade1 = {88, 79, 59};
        int[] grade2 = {91, 72, 85};
        int[] grade3 = {85, 84, 87};
        int[] grade4 = {91, 98, 82};
        int[] grade5 = {99, 98, 97};

        // input student info
        addStudent(0, "null", "null", "null@null.com", 0, grade0); // filler to align student ID with array number
        addStudent(1, "John", "Smith", "John1989@gmail.com", 20, grade1);
        addStudent(2, "Suzan", "Erickson", "Erickson_1990@gmailcom", 19, grade2);
        addStudent(3, "Jack", "Napoli", "The_lawyer99yahoo.com", 19, grade3);
        addStudent(4, "Erin", "Black", "Erin.black@comcast.net", 22, grade4);
        addStudent(5, "Habib", "Trump", "someemail@mywqu.edu", 42, grade5);

        // printing related subroutines/methods
        Student student = students.get(3); //get student 3 for printing
        System.out.println("Print one student:");
        student.print(); //prints specific student data as per 2.D
        printAllStudents(); //print all students as per 3.B
        print_invalid_emails(); //checks email addresses for proper form as per 3.D
        printAverageGrade(); //print grades as per 3.C    
        remove(); // removes student 5 as per 3.A
        printAllStudents(); // shows student 3 has been removed
        remove(); // shows error as student already removed
    }

    public static void addStudent(int studentID, String firstName, String lastName, String email, int age, int[] grades){
            Student newStudent = new Student(studentID, firstName, lastName, email, age, grades);
            students.add(newStudent);
    }

    public static void remove(){ // remove student 
            try{
                System.out.println("Remove student: 5 ");
                students.remove(5);
            } catch (IndexOutOfBoundsException e) {
                System.err.println("IndexOutOfBoundsException: No such student ID");
    }}

    public static void printAverageGrade(){ // print Average Grade
                System.out.println("Print Average Grade: ");
            for(Student eachItem : students){
                double total = eachItem.getgrades()[0] + eachItem.getgrades()[1] + eachItem.getgrades()[2];
                double average = total / 3;
                System.out.println(average);
    }}

    public static void printAllStudents(){ // print All Students
                System.out.println("Print all Students: ");
            for(int i=0; i < students.size(); i++){
                students.get(i).print();    
    }}

    public static void print_invalid_emails(){ // check Email Addresses for proper form
            System.out.println("Check for proper form of email addresses: ");       
            for(Student student : students) {
            if (student.getemail().contains("@") && student.getemail().contains(".") && !student.getemail().contains(" ")){
                System.out.println("Passed format check");
            }
            else
            {
                System.out.println("Invalid email address");
            }}
    }}

public class Student extends Roster
{
   private int studentID;
   private String firstName;
   private String lastName;
   private String email;
   private int age;
   private int[] grades = new int[3];

   public Student(int studentID, String firstName, String lastName, String email, int age, int[] grades)
   {
       setstudentID(studentID);
       setfirstName(firstName);
       setlastName(lastName);
       setemail(email);
       setage(age);
       setgrades(grades);
   }           
   public int getstudentID(){
       return studentID;
   }
   public void setstudentID(int studentID)
   {
       this.studentID = studentID;
   }   
   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 String getemail(){
       return email;
   }
   public void setemail(String email)
   {
       this.email = email;
   }
   public int getage(){
       return age;
   }
   public void setage(int age)
   {
       this.age = age;
   }
   public void setgrades(int[] grades){
       this.grades = grades;
   }
   public int[] getgrades(){
       return grades;
   }
   public void print(){
       System.out.println(
       "Student ID = \t" + getstudentID() + ", \t" +
       "First Name = \t" + getfirstName() + ", \t" +
       "Last Name = \t" + getlastName() + ", \t" + 
       "Email = \t" + getemail() + ", \t" +
       "Age = \t" + getage());
   }
}
端木高邈
2023-03-14

我试图根据这两个要求(数组、平均值)修复您的代码。请参阅以下代码(我已经测试过)。

学生班级:

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
 类似资料:
  • 问题内容: 当我尝试将double数组转换为Double arrayList时,出现以下错误: 线程“主”中的异常java.lang.ClassCastException:[无法将D强制转换为java.lang.Double 下面是我的代码。 我正在将此列表与另一个列表进行比较,并将结果分配给另一个double变量。 请让我知道此错误的原因。 问题答案: las,不适用于原语。Apache com

  • 这一点总的来说 谁能告诉我我哪里错了,这不是重复的答案,我试了所有的可能,我已经通过网络,但没有一个给我正确的答案,如果你能帮助我,我很高兴。这是我的完整代码 错误:它有点笨拙,它在这里是完整的

  • 我在这个问题上卡住了,我做了一个类,其中只有两个属性名称和年龄。我把那个人的详细信息作为用户输入,并保存为中的对象,但是我想将用户从键盘输入的对象属性移动到单独的数组中,就像所有人的名字都移动到数组中一样,所有年龄的人都从ArrayList中移动到数组中。有没有办法做到这一点?代码:

  • 问题内容: 我面临与转换为相关的问题。实际上,我将浮点类型存储在数据库中,但是当我们通过下面的代码从数据库中获取数据时,它是类型。我们获得的价值以表格的形式出现。 那么我们如何获得像这样的格式数据呢? 问题答案: 只需将您的双曲投向花车即可。 还要注意,基本类型不能存储无限的数字集:

  • 问题内容: 我正在实现一个接口,该接口的功能类似于可以包含某种对象的表的功能。该接口指定以下功能: 在我的实现中,我感到很困惑的是,我将表数据存储在2D 数组()中。当我需要返回值时,我想执行以下操作(假定只在包含double的列上调用,因此将没有): 但是- Java不允许强制转换为。将其强制转换为可以,因为它是一个对象,而不是基元,但是我的接口指定数据将以形式返回。 所以我有两个问题: 有什么

  • 问题内容: 在Java中,我想将双精度型转换为整数,我知道您是否这样做: 您得到y = 1。如果您这样做: 您可能会得到2。但是,我想知道:由于整数的双精度表示有时看起来像1.9999999998之类,所以通过Math.round()创建的双精度转换是否仍会导致截断的向下数字,而不是比我们要查找的舍入数(即:1而不是所示代码中的2)? (是的,我的确这样表示:x 是否有 任何 值,其中y将显示x的