我正在为我的课程学习 Java,但我遇到了障碍。我的任务是开发一个简单的命令行程序。为了使事情变得更容易,我得到了以下示例代码来修改,这样我就不必从头开始了。
package assignment;
public class Main {
private final static String[] mainMenuOpts = {"Students","Lecturers","Admin","Exit"};
private final static String[] studentMenuOpts = {"Add Student","List all Students","Find a Student","Return to Main Menu"};
private Menu mainMenu = new Menu("MAIN MENU",mainMenuOpts);
private Menu studentMenu = new Menu("STUDENT MENU",studentMenuOpts);
private DataStore data = new DataStore();
private java.io.PrintStream out = System.out;
private ReadKb reader = new ReadKb();
/** Creates a new instance of Main */
public Main() {
run();
}
private void run(){
int ret = mainMenu.display();
while(true){
switch(ret){
case 1: students();break;
case 2: lecturers(); break;
case 3: admin(); break;
case 4: exit(); break;
}
ret = mainMenu.display();
}
}
private void students(){
int ret = studentMenu.display();
while(ret != 4){
switch(ret){
case 1: addStudent();break;
case 2: listStudents(); break;
case 3: findStudent(); break;
}
ret = studentMenu.display();
}
}
private void lecturers(){
out.println("\nLecturers not yet implemented");
}
private void admin(){
out.println("\nAdmin not yet implemented");
}
//Student methods
private void addStudent(){
out.println("\n\tAdd New Student");
//prompt for details
//add student to the datastore
//ask if they want to enter another student -
// if so call addStudent again
//otherwise the method completes and the studentMenu will display again
}
private void listStudents(){
out.println("\n\tStudent Listing");
//list all students from the datastore
}
private void findStudent(){
out.println("\n\tFind Student");
out.print("Enter Search String: ");
//reasd search text
//use datastore method to get list of students that contain the search string
//display matching students
}
// end Student methods
private void exit() {
data.save(); //call the datastore method that will save to file
out.println("\n\nGoodbye :)");
System.exit(0);
}
}
我正在使用 NetBeans,当我尝试运行项目时出现此错误:
Error: Main method not found in class assignment.Main, please define the main method as: public static void main(String[] args)
我只想让程序运行,以便更好地理解代码。我理解这个错误,但不知道在这堵文字墙的哪里实现 main 方法。我已经尝试了几个小时,但显然作为一个新手我完全没用。任何帮助将不胜感激。