我必须创建一个工资/奖金计算器。

根据输入的薪水,奖金将更高或更低/
例如。低于20000的将是7%,超过该数字的将是5.5%。

最后,我要显示奖金的总额和奖金的薪金总额。
程序完成后,我在总数上遇到了麻烦。

这是我的代码。

class Salaries
{

    public static void main(String agrs[])
    {

        int salary; // this takes in each salary
        double salaryTotal = 0; // this adds the bonus to to salary
        double bonus = 0; // this hold the bonus amount.
        double sumSalary=0;
        double sumBonus=0;
        String exit = "y";// This is the string to be entered to exit the loop.

        // below I'm using a do/while loop to keep it going till a key that isn't "y is entered"
        do
        {

        // the try/catch makes sure that only a number is entered.
        try{
            // below prompts the user to enter the salary amount
        System.out.println("Enter an Employes wages.");
        // below takes in the salary
        salary = EasyIn.getInt();

        // if else ensure that the salary is not below 0
        // and to determine if the amount of bonus to be added.
        if (salary <=-1)
        {
        System.out.println("The salary can not be less than 0");
        }

        else if  (salary >0 && salary <=20000)
            {
            bonus = salary*7/100; // This takes the entered salary and calculates the bonus.
            salaryTotal = salary + bonus;
            System.out.println("The bonus paid is " + bonus);
            System.out.println("********************");
            System.out.println("The total salary + bonus is " + salaryTotal);
            System.out.println("********************");
            System.out.println("Enter y to enter another Employes wages. ");

            System.out.println("Press any other letter to exit.");
            System.out.println("********************");
            System.out.println();
            exit = EasyIn.getString();

            }
        else if (salary >20000 && salary <=30000)
            {
            bonus = salary*5.5/100; // This takes the entered salary and calculates the bonus.
            salaryTotal = salary + bonus;
            System.out.println("The bonus paid is " + bonus);
            System.out.println("********************");
            System.out.println("The total salary + bonus is " + salaryTotal);
            System.out.println("********************");
            System.out.println("Enter y to enter another Employes wages. ");

            System.out.println("Press any other letter to exit.");
            System.out.println("********************");
            System.out.println();
            exit = EasyIn.getString();


            }

        else if (salary >30000 && salary <=40000)
            {

            bonus = salary*4/100;
            salaryTotal = salary + bonus; // This takes the entered salary and calculates the bonus.
            System.out.println("The bonus paid is " + bonus);
            System.out.println("********************");
            System.out.println("The total salary + bonus is " + salaryTotal);
            System.out.println("********************");
            System.out.println("Enter y to enter another Employes wages. ");

            System.out.println("Press any other letter to exit.");
            System.out.println("********************");
            System.out.println();
            exit = EasyIn.getString();


            }

        else if (salary >40000)
            {
            bonus = salary*3.5/100;
            salaryTotal = salary + bonus; // This takes the entered salary and calculates the bonus.
            System.out.println("The bonus paid is " + bonus);
            System.out.println("********************");
            System.out.println("The total salary + bonus is " + salaryTotal);
            System.out.println("********************");
            System.out.println("Enter y to enter another Employes wages. ");

            System.out.println("Press any other letter to exit.");
            System.out.println("********************");
            System.out.println();
            exit = EasyIn.getString();



            }




        }
        catch(Exception e)
        {
            System.out.println("ERROR!!!! Please enter a number.");
        }


        }


        while(exit.equals("y"));


        System.out.println("The total amout of bonus is " + sumBonus );
        System.out.println("The total of all the salaries is " + sumSalary);



    }




}

最佳答案

使这个加倍。 int和double之间的除法可能导致返回整数。

int salary; // this takes in each salary


有关更多信息:http://www.cs.umd.edu/~clin/MoreJava/Intro/expr-int-div.html

同样,进行奖金计算会更加有效:

bonus = salary * 0.055;


我也没有看到您将值从奖金移动到sumBonus的位置:

System.out.println("The total amout of bonus is " + sumBonus );


也许将sumBonus更改为bonus,不要忘记删除声明。

10-06 10:54