2015-02-24 128 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

相關問題