gpt4 book ai didi

java - 创建工资/奖金计算器

转载 作者:行者123 更新时间:2023-12-01 23:23:52 25 4
gpt4 key购买 nike

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

根据输入的薪水,奖金会更高或更低/例如。 20000以下为7%,20000以上为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;

我也没有看到您在哪里将这一行的值从 Bonus 移至 sumBonus:

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

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

关于java - 创建工资/奖金计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com