我已经看过这里,也有类似问题的答案,但是我无法使其符合我的代码。我的代码:
学生班/ StudentArrayList班
import java.util.*;
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 EMail: %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);
}
}
public class StudentArrayList {
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","Tom","Gaye","[email protected]", 65, 99, 98, 97));
//Example of printing specific student data using getters.
System.out.println("");
for (Student a: StudentArray) {
System.out.println(a.getStuID());
System.out.println(a.getFName());
System.out.println(a.getLName());
}
}
}
名册类
public class Roster {
public static void main(String[]args){
System.out.println("");
for (Student s: StudentArray) { //THIS IS WHERE IT ERRORS
System.out.printf("%s\n",s);
}
}
}
我遇到的问题是我正在尝试在
System.out.print
类中执行Roster
。我收到的错误是Roster
类中的“ StudentArray无法解析为变量”。如果我在main
的StudentArrayList
方法下执行此操作,则打印方法可以正常工作。我知道我必须有一种方法来调用数组,只是不确定该怎么做。那会是吸气剂吗? 最佳答案
“ StudentArray无法解析为变量”
这意味着您尚未声明变量StudentArray
。
public class Roster {
public static void main(String[]args){
ArrayList<Student> StudentArray = new ArrayList<Student>();
for (Student s: StudentArray) { // loop through the array
System.out.printf("%s\n",s);
}
}
}