我想遍歷我的「print_average_grade」方法,並打印ArrayList中所有學生的AVG成績。我已經在Main中爲「Roster」類嘗試了一個「for循環」,並且得到一個錯誤,指出「Roster類型中的方法print_average_grade(String)不適用於參數(Student)」。循環使用ArrayList爲所有學生打印AVG成績
我的代碼;
學生類別:
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","[email protected]", 20, 88, 79, 59));
studentArray.add(new Student("2","Susan","Erickson","[email protected]", 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","[email protected]", 22, 91, 98, 82));
studentArray.add(new Student("5","Captain","Planet","[email protected]", 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和因爲質量的材料已經被多次更改。如果不是這個網站的解釋,我會迷路。謝謝..
通's.getStuID()'的方法。 –
而且它也會更好地使用列表的等級。 – maraca