这是我尝试过的Java程序。但是我找不到代码的错误

时间:2016-06-26 07:15:37

标签: java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package exams;

public class ExamDetails {

    public static void main (String[] args){

        StudentResults aStudent = new StudentResults();


        String sName = aStudent.fullName("Bill Gates");
        System.out.println(sName);

    }

    class StudentResults{

    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;

    StudentResults(){
        Full_Name = "No Name Given";
        Exam_Name = "Unknown";
        Exam_Score = "No Score";
        Exam_Grade = "Unknown";
    }

    String fullName(String aName){

        Full_Name = aName;
        return Full_Name;

    }

}

}

3 个答案:

答案 0 :(得分:0)

StudentResults aStudent = new ExamDetails().new StudentResults(); 

在主要功能中执行此操作。

StudentResultExamDetails类的内部类,它不是static

因此,要在静态main函数中使用非静态成员,需要引用包含该成员的类。

答案 1 :(得分:0)

进行以下更改:

public class ExamDetails {

    static  StudentResults aStudent = new StudentResults();
    public static void main (String[] args){




        String sName = aStudent.fullName("Bill Gates");
        System.out.println(sName);

    }

    static class StudentResults{

    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;

    StudentResults(){
        Full_Name = "No Name Given";
        Exam_Name = "Unknown";
        Exam_Score = "No Score";
        Exam_Grade = "Unknown";
    }

    String fullName(String aName){

        Full_Name = aName;
        return Full_Name;

    }

}

}

或者第二个选项是在单独的文件中声明StudentResults

答案 2 :(得分:0)

另一种选择:

package various;

public class  StudentResults{

    public static void main (String[] args){

        StudentResults aStudent = new StudentResults();

        String sName = aStudent.fullName("Bill Gates");
        System.out.println(sName);

    }

    private String Full_Name;
    private String Exam_Name;
    private String Exam_Score;
    private String Exam_Grade;

    StudentResults(){
        Full_Name = "No Name Given";
        Exam_Name = "Unknown";
        Exam_Score = "No Score";
        Exam_Grade = "Unknown";
    }

    String fullName(String aName){

        Full_Name = aName;
        return Full_Name;

    }
}