import java.util.*;
interface Payable {
public void getAmount(int x);
}
class HEmployee implements Payable {
public void getAmount(int x) {
System.out.println("Amount paid to Hourly employee is Rs.::" + (x * 120));
}
}
class WEmployee implements Payable {
public void getAmount(int x) {
System.out.println("Amount paid to Weakly employee is Rs.::" + (x * 820));
}
}
class J40 {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
Payable refer;
HEmployee h1 = new HEmployee();
System.out.println("Enter the Hourly employee worked Hours::");
int h = s.nextInt();
refer = h1;
refer.getAmount(h);
WEmployee w1 = new WEmployee();
System.out.println("Enter Weakly employee worked Hours::");
int w = s.nextInt();
refer = w1;
refer.getAmount(w);
}
}
在 eclipse 中成功运行,但在命令提示符下抛出异常。
按名称J40.java
javac J40.java
成功。
java J40
抛出异常
答案 0 :(得分:0)
我可以通过命令提示符运行代码而不会出现任何异常
我没有修改过。
答案 1 :(得分:0)
我设法在Eclipse和命令提示符下运行代码。我已将每个接口和类划分为单个文件,import java.util.*;
应该在J40类中。
以下是我运行的命令行。
C:\Test>javac J40.java
C:\Test>java J40
Enter the Hourly employee worked Hours::
12
Amount paid to Hourly employee is Rs.::1440
Enter Weakly employee worked Hours::
2
Amount paid to Weakly employee is Rs.::1640