我看过这里有类似问题的答案,但我无法将它们与我的代码相符。我的代码:
Student Class / StudentArrayList Class
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","John1989@gmail.com", 20, 88, 79, 59));
StudentArray.add(new Student("2","Susan","Erickson","Erickson_1990@gmail.com", 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","Tom","Gaye","tgay@att.net", 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
方法下执行此操作,则print方法可以正常工作。我知道我必须有办法调用阵列,只是不确定该怎么做。这会是一个吸气剂吗?
答案 0 :(得分:1)
&#34; StudentArray无法解析为变量&#34;
这意味着您尚未声明变量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);
}
}
}
答案 1 :(得分:1)
您需要先声明studentArray
ArrayList<Student> studentArray = new ArrayList<Student>();
填充添加一些学生,例如:
Student stud1 = new Student("stud params");
//other students declaration
//you could do this with a loop if data are asked to user
studentArray.add(stud1);
然后打印整个列表:
for(Student s: studentArray){
System.out.println(%s\n",s);
}
答案 2 :(得分:1)
我认为你根本不需要Roster类,因为你想让程序在Student类中完成。你不能有两个带有main的类,Student Class和Roster Class都有Main。
学生班的主要是做正确的事,它正在初始化学生并打印名册。
所以你可以做两件事删除Roster类或取出这段代码
public class StudentArrayList {
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@gmail.com", 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","Tom","Gaye","tgay@att.net", 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());
}
}
到名册,这将完成这项工作。