【问题标题】:Main method error can't figure out how to fix [closed]主要方法错误无法弄清楚如何修复[关闭]
【发布时间】:2021-06-07 16:24:36
【问题描述】:

获取仅影响我的检索薪水方法的主要方法错误我不确定如何解决该问题我添加了特定项目,但我无法让错误消失。是我的程序中唯一出现错误并且我无法继续的事情。图片包含我现在遇到的唯一问题

"错误:类中找不到主方法,请将主方法定义为: 公共静态无效主要(字符串 [] 参数) 或者 JavaFX 应用程序类必须扩展 javafx.application.Application"

Image included

public class finalproject {

public static class employeeCase {
       EMPLOYEE[] employees;
       int AMOUNT;
      
       employeeCase(){
           employees = new EMPLOYEE[100];
           AMOUNT = 0;
           
       }

private void loadEmployee() {
    
    
    String ID = null;
       
    int SALARY = 0;
    
    Scanner sc = new Scanner(System.in);
    
    System.out.println(" ");
    System.out.println("How many employees do you want to load?: ");
    int num = sc.nextInt();
    
    for(int i = 0; i < num; i++){
        
        // Display parallel arrays
        System.out.println(" ");
        System.out.println("Name: " + employees[i] + " " + "ID: " + ID + " " + "Salary: " + SALARY);
        
        sc.close();
    }

}
private int addEmployee() {
    
    Scanner sc = new Scanner(System.in);
    
    System.out.println(" ");
    System.out.println("How many employees do you want to enter?: ");
    AMOUNT = 0;
    AMOUNT = sc.nextInt();
    
    String Again1 = "no";
    
    String Fname;
    
    String ID;
       
    int SALARY = 0;
    
    do {
        
        for(int i = 0; i < AMOUNT; i++) {
            
    
     
    System.out.printf("Enter employee's first name: ");
    Fname = sc.nextLine();
    System.out.printf("Enter employee ID (5 digits): ");
    ID = sc.next();
    System.out.printf("Enter employee salary: ");
    SALARY = sc.nextInt();
    
    
    System.out.println(" ");
    
    this.employees[this.AMOUNT] = new EMPLOYEE(Fname, ID, SALARY);
    this.AMOUNT++;
    
    sc.close();
    
        }   } while (Again1.equalsIgnoreCase("yes"));
    return SALARY;
    
}

private void displayEmployee() {

    

    for(int i = 0; i < AMOUNT; i++){
    
        // Display parallel arrays
        System.out.println(" ");
        System.out.println(this.employees[i]);
        
    }

}

private void retrieveSpecific() {
    
    Scanner sc = new Scanner(System.in);
    
    System.out.println(" ");
    System.out.println("Enter employee ID: ");
    String id = sc.next();
    
    //Search for ID in all the stored employees
       for(int i=0; i<this.AMOUNT; i++) {
           
       
           if(id.contentEquals(this.employees[i].ID)) {
               System.out.println(this.employees[i]);
               
               sc.close();
    }}}


private void retrieveSalary() {
        
        Scanner scan = new Scanner(System.in);
        System.out.println(" ");
        System.out.println("Enter lowest employee salary: ");
        int LSALARY = scan.nextInt();
        
        System.out.println(" ");
        System.out.println("Enter highest employee salary: ");
        int HSALARY = scan.nextInt();
        
        for(int i = 0; i < AMOUNT; i++) {
            
        
            if(employees[i].SALARY >= LSALARY & employees[i].SALARY <= HSALARY) {
                   System.out.println(employees[i]);
        
                   scan.close();}}

}

    public static void main(String[] args) {
    
    employeeCase EmployeeData = new employeeCase();

    Scanner sc = new Scanner(System.in);
    
    int Select = 0;
    
    do {
        
        displayMenu();
        
        System.out.print("Input your selection from the menu: ");
        Select = sc.nextInt();
        
    switch (Select) {
    
    case 1 :    EmployeeData.loadEmployee();
                break;
                
    case 2 :    EmployeeData.addEmployee();
                break;
                
    case 3 :    EmployeeData.displayEmployee();
                break;
                
    case 4 :    EmployeeData.retrieveSpecific();
                break;
                
    case 5 :    EmployeeData.retrieveSalary();
                break;
                
    case 6 :    System.out.println("Thank you, goodbye!");
                break;
                
    
    default :   System.err.println("Invalid Input");
                break;
    }

    
    } while (Select != 6);
    
    
    
    sc.close();
}
        
        public static void displayMenu() {
            
            System.out.println("                    MENU");
            System.out.println("============================================");
            System.out.println("1: Load employees' data");
            System.out.println("2: Add new employee");
            System.out.println("3: Display all employees");
            System.out.println("4: Retrieve specfic employee data");
            System.out.println("5: Retrieve employee based on salary range");
            System.out.println("6: Exit program");
        }
}}

【问题讨论】:

  • 主要方法是静态的!!
  • 认真对待错误信息:你没有public static void main (String[] args) 方法!
  • 好的,我现在已经编辑了主要方法,但只有我的 retreivesalary 不适用于案例

标签: java arrays object methods


【解决方案1】:

您的主要方法必须是静态的。将public void main 更改为public static void main

【讨论】:

  • 一旦我改变它我得到一个错误删除静态
  • 主要方法必须是静态的,没有例外。如果错误告诉您它不能是静态的,那是因为您在其中有一个动态引用。您需要通过删除动态引用来解决这个问题,而不是通过使方法动态化。动态引用的示例包括访问非静态的实例变量,以及调用其他非静态的方法。
  • 好的,我现在已经编辑了主要方法,但只有我的 retreivesalary 不适用于案例
  • 听起来我解决了您最初提出的问题,并且您有一个后续问题。在这种情况下,stackoverflow 希望您遵循的过程是通过单击复选标记将我的答案标记为已接受,如果您想问其他问题,请创建一个新问题。如果您像这样编辑它,当人们看到您的问题时会感到困惑,因为现在您的代码 sn-p 似乎包含的代码不存在您所询问的问题。
  • 我的代码仍然存在问题我添加的错误照片仍然存在,并显示相同的错误消息
【解决方案2】:

主方法的访问修饰符应该是静态的,在这里你定义一个非静态的主方法

将您的主要方法替换为 公共静态 void main(String[] args){}

【讨论】:

  • 好的,我现在已经编辑了主要方法,但只有我的 retreivesalary 不适用于案例
猜你喜欢
  • 1970-01-01
  • 2013-07-31
  • 2014-11-07
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多