我創建了一個加班計算器,如果用戶輸入的時間超過49小時並回到用戶輸入使用小時的位置,則會向用戶發出警告有效的。如何將用戶循環回到具有兩個循環的程序開始
如果有人輸入正確的小時數並想要發出另一個查詢,我該如何在循環內發出循環?因爲它目前已經設置在一個循環中。
特異性:
System.out.println ("Would you like to view another account? Y/N: ");
inputLine.next().charAt(0);
下面是完整的代碼診斷:
import java.util.Scanner;
public class Overtime
{
public static void main()
{
Scanner inputLine = new Scanner(System.in);
String staffName;
int i = 1;
double hoursWorked = 0, hourlyRate = 15, totalPay = 0, nationalInsurance = 0, tax = 0, netPay = 0, overtime = 0;
System.out.print("Enter employee name: ");
staffName = inputLine.nextLine();
do
{
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! ");
}
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);
i++;
System.out.println("Would you like to view another account? Y/N: ");
inputLine.next().charAt(0);
//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))));
}
}
while(i == 1);
}
}