【问题标题】:create a salary/bonus calculator创建工资/奖金计算器
【发布时间】:2013-12-01 05:04:40
【问题描述】:

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

根据输入的薪水,奖金会更高或更低/ 例如。 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/int 都是错误的(并且会导致 整数除法 并失去重要的精度),将其更改为 float/int (以便它使用 fp 除法),例如7/100 -> 7.0/100。以这种方式表达5.5/100 已经是正确的。

标签: java


【解决方案1】:

把它变成双倍。 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;

我也没有看到您将这一行的值从 Bonus 移到 sumBonus 的位置:

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

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

【讨论】:

  • * 0.055 并不比写* (5.5/100) 更“高效”。编译器将折叠这样的常量表达式。 (即使没有,在这种情况下,执行时间的差异也完全无关紧要。)
  • 对不起,优雅是我的意思。对于您的示例,字节码将是相同的,而不是他的。他写的时候没有括号,这意味着该值不再是一个常数,因为乘法首先应用于局部变量。仅此一项就会导致额外的 4 字节字节码。至于这个例子,它是无关紧要的,尽管优化仍然是一个早期培养的好习惯。我使用 jclasslib BC 查看器手动测试了这个理论。另见this
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
相关资源
最近更新 更多