gpt4 book ai didi

java - 加类计算器,创建循环的问题

转载 作者:行者123 更新时间:2023-12-02 03:50:59 26 4
gpt4 key购买 nike

我已经设法让我的计算器正常工作,但我试图引入一个循环来允许用户运行第二次搜索。我正在尝试使用 Char(0) 作为 Y/N 答案,但任何其他方法使其工作都会很棒

import java.util.*;

public class Overtime
{
public static void main ()
{
Scanner inputLine = new Scanner(System.in);

String staffName;

double hoursWorked = 0, hourlyRate = 15, totalPay=0, nationalInsurance=0, tax=0, netPay=0, overtime=0;


do{
System.out.print ("Enter employee name: ");

staffName = inputLine.nextLine();

System.out.print ("Enter number of hours worked: ");

hoursWorked = inputLine.nextFloat();

if ( hoursWorked <= 36 )
{
totalPay = (hourlyRate * hoursWorked);
}
else if ( hoursWorked >= 37 && hoursWorked <= 40)
{
totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5);
}
else //(hoursWorked > 41 )
{
totalPay = (hourlyRate * 36) + (41-36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2);
}

if ( hoursWorked >= 37 && hoursWorked <= 40)
{
overtime = (hoursWorked - 36) * (hourlyRate * 1.5);
}
else if ( hoursWorked > 41)
{
overtime = (41-36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate *2);
}

if (totalPay > 155 )
{
nationalInsurance = (totalPay * 0.12);
}


tax = (totalPay * 0.20);
netPay = (totalPay - tax - nationalInsurance);

if (hoursWorked >= 49 )
{
System.out.println ("You are not legally allowed to work over 48 hours! Please enter correct hours: ");
}

else
System.out.println ( "***********************");
System.out.println ("Employee: " + staffName);
System.out.println ("Total Hours Worked: " + hoursWorked);
System.out.println ("Overtime Pay: " + overtime);
System.out.println ("Net Pay: " + totalPay);
System.out.println ("Tax: " + tax);
System.out.println ("National insurance: " + (nationalInsurance = Math.round(nationalInsurance * 100.00 )/ 100.00));
System.out.println ("Net Pay" + netPay);
//System.out.println ("Overtime Pay" + (hoursWorked - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2));
//System.out.println ("Total Deductions " + (totalPay * (20/100) + (totalPay * (4/100))));
}
}
System.out.println ("Would you like to try another user");

}

最佳答案

您只需将逻辑包含在 do while 循环中,下面是一个示例:

boolean exit = false;
do{
//your code
System.out.println("Do you want to add more?");
String choice = sc.next();
if("N".equalsIgnoreCase(choice)){
exit = true;
}
}while(!exit);

关于java - 加类计算器,创建循环的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35870731/

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