繁体   English   中英

静态错误:此类没有接受String []的静态void main方法。

[英]Static Error: This class does not have a static void main method accepting String[].

我的代码编译正常,但是当我运行它时,出现了以下错误:

`Static Error: This class does not have a static void main method accepting String[]`

这是我的代码:

class Employee{
  private String name; //name reference field
  private int idNumber; //integer variable holding employee's ID number
  private String department; //holds the name of the employees department
  private String position; //holds the name of the employee's position in the department
  //the following is the first required constructor
  //using the terms n,i,d, and p to assign values to the above fields
  public Employee(String n, int i, String d, String p){
    name=n;
    idNumber=i;
    department=d;
    position=p;
  }
  //the following is a constructor that sets the fields blank to be filled in
  public Employee(){
    name="";
    idNumber=0;
    department="";
    position="";
  }
  //writing appropriate accessor and mutator methods for all the fields
  public void setName(String n){ name=n; }
  public void setIdNumber(int i){ idNumber = i; }
  public void setDepartment(String d){ department = d; }
  public void setPosition(String p){ position = p; }
  public String getName(){ return name; }
  public int getIdNumber(){ return idNumber; }
  public String getDepartment(){ return department; }
  public String getPosition(){ return position; }
}
//new program creating employee objects
public class EmployeeInfo 
{
  public static void main(String[] args)
  {
    //create an Employee object
    Employee employee1 = new Employee("Susan Meyers", 47899, "Accounting", "Vice President");
    //test the accessor methods
    System.out.println("***employee1***");
    System.out.println("Name: " + employee1.getName());
    System.out.println("ID number: " + employee1.getIdNumber());
    System.out.println("Department: " + employee1.getDepartment());
    System.out.println("Position: " + employee1.getPosition());
    //create another Employee object
    Employee employee2 = new Employee();
    //test the mutator methods
    employee2.setName("Mark Jones");
    employee2.setIdNumber(39119);
    employee2.setDepartment("IT");
    employee2.setPosition("Programmer");
    System.out.println("\n***employee2***");
    System.out.println("Name: " + employee2.getName());
    System.out.println("ID number: " + employee2.getIdNumber());
    System.out.println("Department: " + employee2.getDepartment());
    System.out.println("Position: " + employee2.getPosition());
    //create another Employee object
    Employee employee3 = new Employee();
    //test the mutator methods
    employee2.setName("Joy Rogers");
    employee2.setIdNumber(81774);
    employee2.setDepartment("Manufacturing");
    employee2.setPosition("Engineer");
    System.out.println("\n***employee3***");
    System.out.println("Name: " + employee3.getName());
    System.out.println("ID number: " + employee3.getIdNumber());
    System.out.println("Department: " + employee3.getDepartment());
    System.out.println("Position: " + employee3.getPosition());
  }
}

我知道我需要在某处添加“ public static void main(String [] args)”命令,但我不确定在哪里! 感谢您提供的任何帮助!

您正在创建两个不同的类Employee和EmployeeInfo,我假设您尝试执行Employee类,在该类中编译器无法找到Public static void main

首先,您应该编译所有“ .java”文件。 您可以使用命令行作为

javac *.java 

其次,您应该运行具有“ main”方法的类。

java EmployeeInfo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM