2015-02-24 129 views
0

我使用NetBeans和我的代码如下我不断收到一个错误说“无法找到或加载主类gradebooktest.GradeBookTest”我不知道为什么

public class GradeBook 
{ 
    private String courseName; // course name for this GradeBook 
    private String courseInstructor; // instructor name for this GradeBook 

// constructor initializes courseName and courseInstructor with String Argument 
    public GradeBook(String name, String insname) // constructor name is class name 
    { 
     courseName = name; // initializes courseName 
     courseInstructor = insname; // initializes courseInstructor 
    } // end constructor 

    // method to set the course name 
    public void setCourseName(String name) 
    { 
     courseName = name; // store the course name 
    } // end method setCourse 

    // method to retrieve the course name 
    public String getCourseName() 
    { 
     return courseName; 
    } // end method getCourseName 

    // method to set the Instructor name 
    public void setInstructorName(String insname) 
    { 
     courseInstructor = insname; // store the Instructor name 
    } // end method setInstructorName 

    // method to retrieve the Instructor name 
    public String getInstructorName() 
    { 
     return courseInstructor; 
    } // end method getInstructorName 

    // display a welcome message to the GradeBook user 
    public void displayMessage() 
    { 
     // this statement calls getCourseName to get the 
     // name of the course this GradeBook represents 
     System.out.println("\nWelcome to the grade book for: \n"+ 
     getCourseName()+"\nThis course is presented by: "+getInstructorName()); 
     System.out.println("\nProgrammed by Jack Friedman"); 

    } // end method displayMessage 
} // end 
+0

你都争相推出无主类的应用程序?听起来很奇怪.. – drgPP 2015-02-24 06:16:41

回答

1

你应该调用此构造函数在你的主要方法类。

创建一个新的类GradeBookTest如下:

public class GradeBookTest { 

    public static void main (String args[]) { 
    GradeBook book = new GradeBook("Math", "T.I."); 
    book.displayMessage(); //To see your results 
    } 

} 

现在你可以启动这个类来查看结果。

+0

我会把这个放在哪里?在另一个文件或在这个地方?谢谢 – 2015-02-24 06:22:22

+0

是的,您应该在与您的模型相同的包中创建另一个类GradeBookTest(如我的示例中所示),或者如果需要,可以在另一个包中创建并导入此POJO(您的BookGrade)类。 – drgPP 2015-02-24 06:23:22

0

请为您的应用程序添加一个静态主要方法。

0

主要方法在哪里?包括下面的代码来运行您的程序 -

public static void main(String[] args) { 
     GradeBook book = new GradeBook("subj1", "instruc1"); 
     book.displayMessage(); 
    } 
0

在Java编程语言中,每个应用程序都必须包含一个main方法,其特征是:

main方法是一个Java程序的入口点,它必须被声明为public,以便它可以从类外部和static访问,以便即使不创建该类的实例或对象也可以访问它。

为了更好的理解,请阅读this

相关问题