我的教授和我似乎无法理解为什么具有-1的标记值的do-while循环没有终止。即使在为employeeID输入-1的sentinel值之后,do-while循环仍继续循环。有人可以帮忙吗?
我的哨兵价值有问题吗?是因为我使用的是switch语句吗?
谢谢。
import java.util.Scanner;
import java.text.DecimalFormat;
public class Ass4_WeeklyPayrollTaxGuideWhileLoopIf_Scanner {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat("#0.00");
int withExempt, employeeID;
double hourlyWage, hoursWorked, currentEarnings, taxFICA, withExemptAmount;
double previousYTD, adjWeeklyIncome, currentYTD, taxFederal, payTakeHome;
String input;
char maritalStatus;
System.out.print("Enter employee id number: ");
employeeID = keyboard.nextInt();
System.out.print("Enter hourly wage: $");
hourlyWage = keyboard.nextDouble();
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextDouble();
System.out.print("Enter number of withholding exemptions: ");
withExempt = keyboard.nextInt();
System.out
.print("Enter marital status: [For married, enter m. " + "For single, enter s.] ");
input = keyboard.next();
maritalStatus = input.charAt(0);
System.out.print("Enter previous year-to-date earnings: $");
previousYTD = keyboard.nextDouble();
while (employeeID != -1) {
if (hoursWorked > 40)
currentEarnings = ((hoursWorked - 40) * hourlyWage * 1.5) + (40 * hourlyWage);
else
currentEarnings = (hourlyWage * hoursWorked);
taxFICA = (currentEarnings * 0.062) + (currentEarnings * 0.0145);
withExemptAmount = withExempt * 51.92;
adjWeeklyIncome = currentEarnings - withExemptAmount;
currentYTD = currentEarnings + previousYTD;
taxFederal = 0;
switch (maritalStatus) {
case 's':
case 'S':
if (adjWeeklyIncome <= 51)
taxFederal = 0;
else if (adjWeeklyIncome <= 517)
taxFederal = ((adjWeeklyIncome - 51) * 0.15);
else if (adjWeeklyIncome <= 1105)
taxFederal = (69.90 + ((adjWeeklyIncome - 517) * 0.28));
else if (adjWeeklyIncome <= 2493)
taxFederal = (234.54 + ((adjWeeklyIncome - 1105) * 0.31));
else if (adjWeeklyIncome <= 5385)
taxFederal = (664.82 + ((adjWeeklyIncome - 2493) * 0.36));
else
taxFederal = (1705.94 + ((adjWeeklyIncome - 5385) * 0.396));
break;
case 'm':
case 'M':
if (adjWeeklyIncome <= 124)
taxFederal = 0;
else if (adjWeeklyIncome <= 899)
taxFederal = ((adjWeeklyIncome - 124) * 0.15);
else if (adjWeeklyIncome <= 1855)
taxFederal = (116.25 + ((adjWeeklyIncome - 899) * 0.28));
else if (adjWeeklyIncome <= 3084)
taxFederal = (383.93 + ((adjWeeklyIncome - 1855) * 0.31));
else if (adjWeeklyIncome <= 5439)
taxFederal = (764.92 + ((adjWeeklyIncome - 3084) * 0.36));
else
taxFederal = (1612.72 + ((adjWeeklyIncome - 5439) * 0.396));
break;
default:
System.out.print("Incorrect entry for marital status.");
}
payTakeHome = currentEarnings - taxFICA - taxFederal;
System.out.print("\n\tPayroll information" + "\n\t-----------------"
+ "\n\tEmployee id: " + employeeID + "\n\tCurrent earnings: $"
+ formatter.format(currentEarnings) + "\n\tYear-to-date earnings: $"
+ formatter.format(currentYTD) + "\n\tFICA taxes: $" + formatter.format(taxFICA)
+ "\n\tFederal income tax withheld: $" + formatter.format(taxFederal)
+ "\n\tTake home pay check amount: $" + formatter.format(payTakeHome));
System.out.print("\n\nEnter employee id number: ");
employeeID = keyboard.nextInt();
System.out.print("Enter hourly wage: $");
hourlyWage = keyboard.nextDouble();
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextDouble();
System.out.print("Enter number of withholding exemptions: ");
withExempt = keyboard.nextInt();
System.out.print(
"Enter marital status: [For married, enter m. " + "For single, enter s.] ");
input = keyboard.next();
maritalStatus = input.charAt(0);
}
}
}