我对计算机编程非常陌生-实际上是在6周前开始的课程-我目前在Netbeans中遇到了非法开始表达式的麻烦。
整个代码如下(因为我甚至不知道从哪里开始):
public class Employees {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
public class Employee
//properties
private String name;
private String ID;
private String salary;
//constructor
public Employee (String name, String address, String dob) {
this.name = name;
this.ID = ID;
this.salary = salary;
}
//method to print details on employees
public void printDetails() {
System.out.println("Employee name: " +this.name);
System.out.println("ID: "+ this.ID);
System.out.println("Annual Salary: " + this.salary);
}
}
}
不能在方法声明(public static void main(String[] args) {
)中声明类(public class Employee
)。
最好将class Employee
的声明移到它自己的文件(Employee.java)中。如果您不想将其移动到另一个文件,您也可以将其移动到现有文件的末尾。但是你必须声明它是private
而不是public
。
或者你可以像这样把所有的东西都做成一个类:
public class Employees {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Employees employee = new Employees("name", "address", "dob");
employee.printDetails();
}
//properties
private String name;
private String ID;
private String salary;
//constructor
public Employees (String name, String address, String dob) {
this.name = name;
this.ID = ID;
this.salary = salary;
}
//method to print details on employees
public void printDetails() {
System.out.println("Employee name: " +this.name);
System.out.println("ID: "+ this.ID);
System.out.println("Annual Salary: " + this.salary);
}
}