-3
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抛出异常

4

2 回答 2

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
于 2013-08-27T05:02:40.843 回答
0

我可以通过命令提示符运行您的代码而不会出现任何异常

在此处输入图像描述

我没有做任何修改。

于 2013-08-27T04:57:31.623 回答