5

抱歉,我是 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”中的主要异常)。

好的,我添加了 public,但现在我得到了这个:

线程“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)
4

1 回答 1

5

您对 main 的问题是它不存在,您需要在程序中添加一个才能运行。将它放在主类中,无论是哪个,但是虽然它需要类中,在定义类的花括号中,但您还必须确保不要将它放在另一个方法中。

上面,我把它放在 TestEmployee 中。

我还要注意确保上面的每个类都被声明为 public 并且在它自己的文件中。所以上面包含 4 个类的代码应该由 4 个文件组成。


编辑
另外,请务必将您的主要方法声明为公共方法,如以下评论中所述的@Aniket。


编辑 2

  • 您仍然没有将 main 声明为公共方法。
  • 您在 main 方法中嵌入了方法。请记住,在 Java 中您不能这样做,因为所有方法都需要是类级别的。让他们脱离主要方法。
  • 您的代码缩进至少可以说是可怕的,这将使您或我们很难看到您的编码问题。您将需要投入时间和精力来正确缩进代码。如果你这样做了,你会立即看到方法内部有方法。
于 2013-11-10T04:20:52.817 回答