0

私のコードは次のとおりです。コードは、配列リストからの関連データを含む学生のリストを返す必要があります。しかし、エラーは、非静的メソッドへの静的参照を作成できないことを示しています??

メソッドを静的にしようとしたところ、別のエラーが発生しました。

//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;
}
  }
4

4 に答える 4

1

StudentClass = studentDetails.get(i);意味がありません。StudentClassクラス名です。

インスタンスが必要です:StudentClass student = studentDetails.get(i);次にstudent.getSurname()etcを使用します。

于 2013-10-23T05:22:37.950 に答える
0

StudentClass.getSurname()静的メソッドとして定義されていません。そのクラスを呼び出せるようにするには、そのクラスのインスタンスを宣言する必要があります。

StudentClass myClass = new StudentClass(/* whatever params you need*/);
String surname = myClass.getSurname();

インスタンスレベルであるため、これらすべてのメソッドに対してこれを行う必要があることに注意してください。

于 2013-10-23T05:21:56.837 に答える
0
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() + "." );
            }

上記のコードを次のように変更します。

    for(int i = 0; i < studentDetails.size(); i++){
        StudentClass student = studentDetails.get(i);
        System.out.println( student.getForename() + " " +
        student.getSurname() + " received a " + student.getGrade() +
                 " for their Student Mark of " + student.getMark() + "." );
   }

エラーがなくなるかどうかを確認します。(クラスのインスタンスではなく) クラス自体でインスタンス レベル (非静的) メソッドを呼び出そうとしたため、コンパイラは不平を言っていました。

于 2013-10-23T05:23:16.783 に答える
0

Java の非静的メソッドは、オブジェクトなしではアクセスできません

このようにしてみるだけで、

else{
         for(int i = 0; i < studentDetails.size(); i++){
             studentDetails.get(i) = new StudentClass();
             System.out.println( studentDetails.get(i).getForename() + " " +
             studentDetails.get(i).getSurname() + " received a " + studentDetails.get(i).getGrade() +
             " for their Student Mark of " + studentDetails.get(i).getMark() + "." );
            }
        }
于 2013-10-23T05:25:14.713 に答える