这是我的代码:该代码应返回包含来自 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;
}
}