gpt4 book ai didi

java - 循环我的 ArrayList 以打印所有学生的平均成绩

转载 作者:行者123 更新时间:2023-12-04 12:55:18 25 4
gpt4 key购买 nike

我想遍历我的“print_average_grade”方法并打印 ArrayList 中所有学生的 AVG 成绩。我在 Main 中为“Roster”类尝试了一个“for 循环”,并收到一条错误消息,指出“Roster 类型中的方法 print_average_grade(String) 不适用于参数 (Student)”。

我的代码;

学生类(class):

public class Student {

private String StuID;
private String FName;
private String LName;
private String Email;
private int Age;
private double Grade1;
private double Grade2;
private double Grade3;


public Student (String stuid, String fname, String lname, String email,
int age, double grade1, double grade2, double grade3)
{
this.StuID = stuid;
this.FName =fname;
this.LName = lname;
this.Email = email;
this.Age = age;
this.Grade1 = grade1;
this.Grade2 = grade2;
this.Grade3 = grade3;

}
public String getStuID(){
return this.StuID;}

public String getFName(){
return this.FName;}

public String getLName(){
return this.LName;}

public String getEmail(){
return this.Email;}

public int getAge(){
return this.Age;}

public double getGrade1(){
return this.Grade1;}

public double getGrade2(){
return this.Grade2;}

public double getGrade3(){
return this.Grade3;}

public String setStuID(String newStuID){
return (this.StuID= newStuID);}

public String setFName(String newFName){
return (this.FName= newFName);}

public String setLName(String newLName){
return (this.LName= newLName);}

public String setEmail(String newEmail){
return (this.Email= newEmail);}

public int setAge(int newAge){
return (this.Age= newAge);}

public double setGrade1(double newGrade1){
return (this.Grade1= newGrade1);}

public double setGrade2(double newGrade2){
return (this.Grade2= newGrade2);}

public double setGrade3(double newGrade3){
return (this.Grade1= newGrade3);}


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

}
}

花名册类:

import java.util.ArrayList;

public class Roster {
static ArrayList<Student> studentArray;

public Roster(ArrayList<Student> ar)
{
studentArray=ar;
}
//3.A - Remove student.
public static void remove(String sdId){
Student stud = null;
for(Student s : studentArray){
if(s.getStuID().equals(sdId))
stud = s;}

if(stud != null)
studentArray.remove(stud);
else
System.out.println("ID does not exist.");
}

//3.B - Print All Student Info
public static void print_all(){
System.out.println("");{
for (Student t: studentArray) {
System.out.printf("%s\n",t);}}

}

//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);
}
}
}

//3.D - Print invalid E-mails
public static void print_invalid_emails(){
for(Student u : studentArray){
if(u.getEmail().contains("@") && u.getEmail().contains(".") && !u.getEmail().contains(" ")){
continue;}
else{
System.out.println(u.getEmail());}
}
}
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","PowIsUrs@planet.net", 65, 99, 98, 97));

//Step 2D; print() specific student data example.
System.out.println("");
for (Student a: studentArray) {
System.out.println(a.getStuID());
System.out.println(a.getFName());
System.out.println(a.getLName());}

new Roster(studentArray);
Roster.print_all();
Roster.print_invalid_emails();

//Works fine for one student.
Roster.print_average_grade("1");

/This loop produces the error.
for (Student v : studentArray) {
print_average_grade(v);
}
Roster.remove("3");
Roster.remove("3");
}
}

我正在参加在线 Java 类(class),由于质量原因, Material 已更改多次。如果没有这个网站的解释,我会迷路的。谢谢..

最佳答案

您的变量“v”是一个 Student,但您的方法参数是 String。更改方法。

public static void print_average_grade(Student v){

double total = v.getGrade1() + v.getGrade2() + v.getGrade3();
double average = total / 3;
System.out.println("Student ID#" + v.getStuID + " Grade AVG= " + average);


}

关于java - 循环我的 ArrayList 以打印所有学生的平均成绩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33632627/

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