以下是我的代碼:代碼應該返回列表中的學生與來自arraylist的相關數據。但是錯誤告訴我它不能對非靜態方法進行靜態引用?如何返回Java類中的ArrayList值
我試圖使方法靜態,它給了我另一個錯誤。
//main function code
String forename = null;
String surname = null;
String grade = null;
String yesOrNo;
double mark;
int selection;
ArrayList<StudentClass> studentDetails = new ArrayList<StudentClass>();
switch(selection){
case 1: {
if (studentDetails.isEmpty()){
System.out.println("No Students Have Been Entered Yet");
main(null);
break;
}
else{
for(int i = 0; i < studentDetails.size(); i++){
StudentClass = studentDetails.get(i);
System.out.println(StudentClass.getForename() + " " +
StudentClass.getSurname() + " received a " + StudentClass.getGrade() +
" for their Student Mark of " + StudentClass.getMark() + ".");
}
}
break;
\\ Error:Exception in thread "main" java.lang.Error: Unresolved compilation problems:
StudentClass cannot be resolved to a variable
Cannot make a static reference to the non-static method getForename() from the type StudentClass
Cannot make a static reference to the non-static method getSurname() from the type StudentClass
Cannot make a static reference to the non-static method getGrade() from the type StudentClass
Cannot make a static reference to the non-static method getMark() from the type StudentClass
at students.main(students.java:60)
\\code for Class
public class StudentClass {
public String Forename;
public String Surname;
public String Grade;
public double Mark;
public StudentClass(String forename, String surname, double mark){
Forename = forename;
Surname = surname;
Mark = mark;
}
public void setForename(String forename)
{
Forename= forename;
}
public void setSurname(String surname)
{
Surname= surname;
}
public void setMark(double mark)
{
Mark= mark;
}
public String getForename()
{
return Forename;
}
public String getSurname()
{
return Surname;
}
public double getMark()
{
return Mark;
}
public String getGrade()
{
if (Mark < 40)
Grade = "FAIL";
else if ((Mark >= 40) && (Mark <= 64))
Grade ="PASS";
else if ((Mark >= 65) && (Mark <= 84))
Grade ="MERIT";
else if ((Mark >= 85) && (Mark <= 100))
Grade ="DISTINCTION";
return Grade;
}
}