很抱歉,我是Java的新手,我的代码存在问题。我已经阅读了线程,并看到了许多关于此特定错误(java.lang.NoSuchMethodError: main Exception in thread "main")
的示例。我似乎无法绕过我要添加(static void main(String[] args)
)代码的地方。如果你们能指出我正确的方向,我会非常感激。
这就是我所拥有的:
public class Employee {
String name;
String department;
double hourlyRate;
Employee(String name, String department, double hourlyRate) {
this.name = name;
this.department = department;
this.hourlyRate = hourlyRate;
}
public void setDepartment(String department) {
this.department = department;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public String getNameAndDepartment() {
return name + " " + department;
}
double weeklyPay(int numOfHourWorked) {
if (numOfHourWorked < 40) {
return (numOfHourWorked * hourlyRate);
} else
return (40 * hourlyRate);
}
}
class UnionEmployee extends Employee {
double dues;
UnionEmployee(String name, String department, double hourlyRate, double dues) {
super(name, department, hourlyRate);
this.dues = dues;
}
public void setDues(double dues) {
this.dues = dues;
}
double weeklyPay(int numOfHourWorked) {
if (numOfHourWorked <= 40) {
return (super.weeklyPay(numOfHourWorked));
} else
return ((super.weeklyPay(40) + ((numOfHourWorked - 40) * hourlyRate * 1.5)) - dues);
}
}
class CommissionEmployee extends Employee {
double commisionRate;
double salesAmount;
CommissionEmployee(String name, String department, double hourlyRate) {
super(name, department, hourlyRate);
}
public void setCommisionRate(double commisionRate) {
this.commisionRate = commisionRate;
}
public void setSalesAmount(double salesAmount) {
this.salesAmount = salesAmount;
}
double weeklyPay(int numOfHourWorked) {
return (super.weeklyPay(numOfHourWorked) + (commisionRate * salesAmount));
}
}
class TestEmployee {
UnionEmployee uEmp = new UnionEmployee(null, null, 0, 0);
CommissionEmployee cEmp = new CommissionEmployee(null, null, 0);
Employee emp = new Employee(null, null, 0);
void display(Employee emp, int numOfHourWorked) {
System.out.println("Name and department :" + emp.getNameAndDepartment ());
System.out.println("Weekly pay of employee :"
+ emp.weeklyPay(numOfHourWorked));
}
void display(UnionEmployee uEmp, CommissionEmployee cEmp,
int numOfHourWorked) {
System.out.println("Weekly Pay for UnionEmployee"
+ uEmp.weeklyPay(numOfHourWorked));
System.out.println("Weekly Pay for UnionEmployee"
+ cEmp.weeklyPay(numOfHourWorked));
}
}
好的,所以我开始将每个类分成不同的文件。在查看Java教程时,它说要添加static void main(String [] args)教程设置的方式是这样的:
public class Misc {
static void main(String[] args) {
//body
}
}
所以我这样做了:
class TestEmployee {
static void main(String[] args) {
UnionEmployee uEmp = new UnionEmployee(null, null, 0, 0);
CommissionEmployee cEmp = new CommissionEmployee(null, null, 0);
Employee emp = new Employee(null, null, 0);
void display(Employee emp, int numOfHourWorked) {
System.out.println("Name and department :" + emp.getNameAndDepartment ());
System.out.println("Weekly pay of employee :"
+ emp.weeklyPay(numOfHourWorked));
}
void display(UnionEmployee uEmp, CommissionEmployee cEmp,
int numOfHourWorked) {
System.out.println("Weekly Pay for UnionEmployee"
+ uEmp.weeklyPay(numOfHourWorked));
System.out.println("Weekly Pay for UnionEmployee"
+ cEmp.weeklyPay(numOfHourWorked));
}
}
}
仍然得到相同的错误:(java.lang.NoSuchMethodError:线程“main”中的主要异常)。
好的我加上公开但现在我明白了:
线程“main”中的异常java.lang.Error:未解决的编译问题:
void is an invalid type for the variable display
Syntax error on token "(", ; expected
Duplicate local variable emp
Syntax error on token ",", ; expected
Syntax error on token ")", ; expected
void is an invalid type for the variable display
Syntax error on token "(", ; expected
Duplicate local variable uEmp
Syntax error on token ",", ; expected
Duplicate local variable cEmp
Syntax error on token ",", ; expected
Duplicate local variable numOfHourWorked
Syntax error on token ")", ; expected
at TestEmployee.main(TestEmployee.java:9)
答案 0 :(得分:5)
你对main的问题是它不存在,你需要在程序中放一个才能运行它。把它放在主类中,无论哪一个,但是当它需要在类的里面时,在定义类的花括号内,你还必须确保你没有把它放在另一种方法中。
上面,我将它放在TestEmployee中。
我还要注意确保上面的每个类都声明为public并且在自己的文件中。因此,上面包含4个类的代码应该由4个文件组成。
修改强>
另外,请务必将主方法声明为 public 方法,如下面的评论中所述@Aniket。
修改2