gpt4 book ai didi

java - 将单独的 "double"值转换为 ArrayList 中的数组

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:41 27 4
gpt4 key购买 nike

我创建了一个程序,其中包含一个 ArrayList,其中包含用于学生成绩的分隔“双”值。然而,该类(class)要求创建一个 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 一部分的数组中?

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

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

谢谢。

附注如果发布完整的代码会更容易,我很乐意发布它以获取有关此问题的任何帮助。

最佳答案

我尝试根据两个要求(数组、AVG)修复您的代码。请看下面的代码(我已经测试过)。

学生类(class):

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

关于java - 将单独的 "double"值转换为 ArrayList 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654457/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com