创建工资/奖金计算器

时间:2013-11-30 20:53:36

标签: java

我必须创建薪水/奖金计算器。

根据输入的工资,奖金会更高或更低/ 例如。 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);



    }




}

1 个答案:

答案 0 :(得分:3)

让它变成双倍。 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转移到sumBonus:

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

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