簡體   English   中英

我一直收到錯誤消息“找不到或加載主類gradebooktest.GradeBookTest”,我不確定為什么

[英]I keep getting an error saying “Could not find or load main class gradebooktest.GradeBookTest” I am not sure why

我正在使用netbeans,我的代碼如下

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

您應該在main方法中調用此類的構造函數。

新建一個新的GradeBookTest類,如下所示:

public class GradeBookTest {

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

}

現在,您可以啟動此類以查看您的結果。

請向您的應用程序添加靜態main方法。

主要方法在哪里? 包括以下代碼以運行您的程序-

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

在Java編程語言中,每個應用程序都必須包含一個主要方法,其簽名為:

public static void main(String[] args)

main方法是Java程序的入口點,必須將其聲明為public以便可以從類外部訪問它,而可以將其聲明為static以便即使不創建類的實例或對象也可以對其進行訪問。

為了更好的理解,請閱讀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM