一个Java程序,显示5名员工的每周薪资(预期输出错误)

时间:2019-03-03 16:47:50

标签: java compiler-errors runtime-error

我的编码基于以下问题: screenshot of the question

这是我编写的代码,已经完成,但是所需的输出出现错误。

package assignment_2;

import java.util.Scanner;
public class Assignment_2 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner (System.in);
        
        String employeeName[] = new String[5];
        int employeeID[] = new int[5];
        int payStatus[] = new int[5];
        double Grosspay[] = new double [5];
        int workHours[] = new int[5];
        double payRateperhour[] = new double [5];
        double incomeTax[] = new double [5];
        double netPay[] = new double [5];
        
        
        
        System.out.println("Enter 5 employee details.");
        
        for (int i = 0; i < 5; i++) {
            String nextLine = input.nextLine(); //skip whitespace error
            System.out.print("Enter Employee " + (i+1) + " name: "); //prompt user input for employee name
            employeeName[i] = input.nextLine();
            System.out.print("Enter Employee " + (i+1) + " ID: "); //prompt user input for employee ID
            employeeID[i] = input.nextInt();
            System.out.print("Enter Employee " + (i+1) + " pay status (1 = Salary or 2 = Hourly): "); //prompt user input for employee paid by salary or hourly?
            payStatus[i] = input.nextInt();
            if (payStatus[i] == 1){ //pay by salary user input
                System.out.print("Enter employee gross pay(RM): ");
                Grosspay[i] = input.nextDouble();
                System.out.print("Enter employee work hours per week: ");
                workHours[i] = input.nextInt();
            } else { //pay by hourly user input
                System.out.print("Enter employee pay rate per hour(RM): ");
                payRateperhour[i] = input.nextDouble();
                System.out.print("Enter employee work hours per week: ");
                workHours[i] = input.nextInt();
                //calculate overtime pay
                if (workHours[i] > 40) {
                    Grosspay[i] = (40 * payRateperhour[i]) + 1.5 * (workHours[i] - 40) * payRateperhour[i];
                } else Grosspay[i] = workHours[i] * payRateperhour[i];

            }
        
        //calculate income tax
        if (Grosspay[i] > 0.00 || Grosspay[i] < 999.99) {
            incomeTax[i] = (8 /100) * Grosspay[i];
        } else if (Grosspay[i] > 500.00 || Grosspay[i] < 999.99) {
            incomeTax[i] = (10 /100) * Grosspay[i];
        } else if (Grosspay[i] > 1000.00) {
            incomeTax[i] = (15 /100) * Grosspay[i];
        } else
            
        //calculate net pay 
        netPay[i] = Grosspay[i] - incomeTax[i];
        
    }
//Display employee details and weekly salary
        System.out.print("\n\n\t\tPrint Daily Company (PDC)\n\t\t   Weekly Payroll Data\n");
        System.out.println("----------------------------------------------------------------");
        System.out.println("   Name\t\tID#\tGross Pay\tTax\tNet Pay");
        for (int i = 0; i < 5; i++) {
            System.out.println(employeeName[i] + "\t" + employeeID[i] + "\t" + Grosspay[i] + "\t" + incomeTax[i] + "\t" + netPay[i]);
                    }
        
    }
    
}

我的错误是它不计算所得税。在输出中显示0.00。净工资与总工资的输出相同。而且我在程序中键入的给定样本的输出之一显示出与其余输出不同的两位小数。

P.s:我也不知道如何解决空白操作系统跳过输入行的错误。所以我只是插入了字符串nextLine = input.nextLine(); //在所有用户输入之前跳过空格错误。 (如果有人可以帮助我,或者教我/给我流畅的输出,那就太好了

谢谢。

下面是我的输出结果,以防万一。 screenshot of output

[更新]: 这是我程序的更新输出。 enter image description here我的净工资无效,所以小数点的值也一样。仍然需要帮助。

谢谢。

1 个答案:

答案 0 :(得分:0)

所以这个小“问题”不那么容易抓住。 但是从本质上讲,Java通过四舍五入来欺骗您。每次您使用(8/100)或任何其他计算所得的所得税时,java都会将它们四舍五入为0,因为整数/整数被解释为整数结果。解决此问题的最简单方法是,每100个添加.0。

    if (Grosspay[i] > 0.00 || Grosspay[i] < 999.99) {
        incomeTax[i] = (8 / 100.0) * Grosspay[i];
    } else if (Grosspay[i] > 500.00 || Grosspay[i] < 999.99) {
        incomeTax[i] = (10 / 100.0) * Grosspay[i];
    } else if (Grosspay[i] > 1000.00) {
        incomeTax[i] = (15 / 100.0) * Grosspay[i];
    }

这应该有效。