遇到MAIN问题请告知

时间:2013-11-10 04:19:56

标签: java

很抱歉,我是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)

1 个答案:

答案 0 :(得分:5)

你对main的问题是它不存在,你需要在程序中放一个才能运行它。把它放在主类中,无论哪一个,但是当它需要在类的里面时,在定义类的花括号内,你还必须确保你没有把它放在另一种方法中。

上面,我将它放在TestEmployee中。

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


修改
另外,请务必将主方法声明为 public 方法,如下面的评论中所述@Aniket。


修改2

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