计算总薪水

时间:2015-08-10 14:27:09

标签: java type-conversion

我有一个问题,我正在努力解决。

在一家公司中,一个emplopyee支付如下: 如果他的基本工资低于卢比。 1500,然后HRA =基本工资的10%,DA =基本工资的90%。 如果他的薪水等于或高于卢比。 1500,然后HRA = Rs。 500和DA =基本工资的98%。如果输入了员工的工资,请写一个程序来查找他的工资总额。

输入

3

1203

10042

1312

预期输出

2406

20383.2

2624

实际输出

1.5668931843000004E8

19579.800000000003

2.0325629952E8

Java代码......

class salary {
    public static void main(String[] args) throws java.lang.Exception {
        // your code goes here
        int testNum;
        int[] testCases;

        Scanner in = new Scanner(System. in );
        testNum = in .nextInt();

        testCases = new int[testNum];

        int i = 0;

        while ( in .hasNextInt()) {
            testCases[i] = in .nextInt();
            //System.out.println(testCases[i]);
            salary(testCases[i]);
            i++;
        }
    }

    public static double salary(double n) {
        double tsal = 1; // this  will be the result
        double hra;
        double da;
        if (n < 1500) {
            hra = 0.1 * n;
            da = 0.9 * n;
            tsal = n + hra + da;
        }
        if (n >= 1500) {
            hra = 500;
            da = 0.9 * n;
            tsal = n + hra + da;
        }

        System.out.println(Math.round(tsal));
        return tsal;
    }

}

我无法理解我哪里出错了。编译器没有显示任何错误。

编辑1:

我在第一个if语句中将行“tsal = n * hra * da”更改为“tsal = n + hra + da”,并将system.output行更改为

 "System.out.println(Math.round(tsal));" 

仍然没有得到所需的输出。知道如何将double值四舍五入到小数点后1位吗?

1 个答案:

答案 0 :(得分:0)

将此行:tsal = n * hra * da;更改为tsal = n + hra + da;

这将有助于&lt; 1500

另外,对于你的RS&gt; 1500,如果DA = 98%,则代码da = 0.9 * n;应为da = 0.98 * n;

相关问题