【问题标题】:Calculate Total salary计算总工资
【发布时间】:2015-08-10 14:27:09
【问题描述】:

我有一个问题正在尝试解决。

在公司中,员工的薪酬如下: 如果他的基本工资低于卢比。 1500,则 HRA = 基本工资的 10%,DA = 基本工资的 90%。 如果他的薪水等于或高于卢比。 1500,然后 HRA = 卢比。 500 和 DA = 基本工资的 98%。如果输入 Employee 的薪水,编写一个程序来计算他的总薪水。

输入

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));" 

我仍然没有得到想要的输出。知道如何将双精度值四舍五入到小数点后 1 位吗?

【问题讨论】:

  • tsal = n * hra * da 那是n * n * n * 0.9 * 0.1,或n * n * n * 0.09。可能不是你想要的。

标签: java type-conversion


【解决方案1】:

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

这将有助于

另外,对于您的 RS > 1500,如果 DA = 98%,那么您的代码 da = 0.9 * n; 应该是 da = 0.98 * n;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    相关资源
    最近更新 更多