[英]I keep getting an error saying “Could not find or load main class gradebooktest.GradeBookTest” I am not sure why
I am using netbeans and my code is as follows 我正在使用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
You should call the constructor of this class in your main method. 您应该在main方法中调用此类的构造函数。
Make a new class GradeBookTest as follows: 新建一个新的GradeBookTest类,如下所示:
public class GradeBookTest {
public static void main (String args[]) {
GradeBook book = new GradeBook("Math", "T.I.");
book.displayMessage(); //To see your results
}
}
Now you can launch this class to view your results. 现在,您可以启动此类以查看您的结果。
请向您的应用程序添加静态main方法。
Where is the main method? 主要方法在哪里? Include below code to run your program-
包括以下代码以运行您的程序-
public static void main(String[] args) {
GradeBook book = new GradeBook("subj1", "instruc1");
book.displayMessage();
}
In the Java programming language, every application must contain a main method whose signature is: 在Java编程语言中,每个应用程序都必须包含一个主要方法,其签名为:
public static void main(String[] args)
main
method is the entry point for a java program, it has to be declared as public
so that it can be accessed from outside the class and static
so that it can be accessed even without creating an instance or object of the class. main
方法是Java程序的入口点,必须将其声明为public
以便可以从类外部访问它,而可以将其声明为static
以便即使不创建类的实例或对象也可以对其进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.