我程序中的所有内容都在工作,但是我只希望如果用户是该程序的一部分,就可以执行退休抵免。现在,它无论如何都在运行。我将如何使它仅在用户输入第二或第三班时输入“ 1”时才运行,而在用户选择“ 2”时不运行。我试图改变我的if语句的顺序,并在其中包括我的方法调用,但这最终给了我很多错误。我认为我的订购错了吗?
import java.util.*;
public class AcmePay {
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many hours did you work this week?");
double hours = keyboard.nextDouble();
System.out.println("What shift did you work? 1, 2, or 3?");
int shift = keyboard.nextInt();
if (shift == 1) {
System.out.println("You worked the 1st shift this week");
}
else {
System.out.println("Do you participate in the retirement program? 1 for yes, 2 for no.");
int retirementProgram = keyboard.nextInt();
if (retirementProgram == 1) {
System.out.println("You particippate in the retirement program, your paycheck will be deducted.");
}
else {
System.out.println("You don't participate in the retirement program, your check did not get deducted");
}
}
double rate = rateForShift(shift);
double paycheck = calculatePayCheck(rateForShift(shift), hours);
double overtime = overtimePay(hours, rate);
double retirement = retirementDeduction(paycheck);
double totalPay = paycheck - retirement + overtime;
double overtimePlusPaycheck = paycheck + overtime;
System.out.println("1. Hours worked " + hours + " hours");
System.out.println("2. You worked the " + shift + " shift");
System.out.println("3. Hourly Pay Rate = $" + rate);
System.out.println("4. Regular pay = $" + paycheck);
System.out.println("5. Overtime Pay = $" + overtime);
System.out.println("6. Total of regular and overtime pay = $ " + overtimePlusPaycheck);
System.out.println("7. Retirement deduction = $" + retirement);
System.out.println("8. Net Pay = $" + totalPay);
}
private static double calculatePayCheck(double rate, double hours) {
if (hours <= 40) {
return hours * rate;
}
else {
return (40 * rate) + ((hours - 40) * (rate * 1.5));
}
}
private static double rateForShift(int shift) {
if (shift == 1) {
return 17;
}
else if (shift == 2) {
return 18.50;
}
else if (shift == 3) {
return 22;
}
return 0;
}
private static double retirementDeduction(double paycheck) {
double retirement = paycheck * .03;
return retirement;
}
private static double overtimePay(double hours, double rate) {
if (hours >= 40) {
return (hours - 40) * (rate * 1.5);
}
else {
return 0;
}
}
}
在方法中使用布尔值标志:
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
boolean inRetirementProgram = false; // flag: Default is false.
// .... The rest of your code ...
}
然后当被问及是否在退休计划中时:
else {
System.out.println("Do you participate in the retirement program? 1 for yes, 2 for no.");
int retirementProgram = keyboard.nextInt();
if (retirementProgram == 1) {
System.out.println("You particippate in the retirement program, "
+ "your paycheck will be deducted.");
inRetirementProgram = true;
}
else {
System.out.println("You don't participate in the retirement program, "
+ "your check did not get deducted.");
}
}
然后声明并初始化双精度类型retirement变量:
/* Ternary Operator used here. Same as doing:
double retirement = 0.0d;
if (inRetirementProgram) { retirementDeduction(paycheck); }
*/
double retirement = (inRetirementProgram ? retirementDeduction(paycheck) : 0.0d);
在开始时移动变量,以便可以根据选项进行计算。
执行以下操作:
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many hours did you work this week?");
double hours = keyboard.nextDouble();
System.out.println("What shift did you work? 1, 2, or 3?");
int shift = keyboard.nextInt();
double rate = rateForShift(shift);
double paycheck = calculatePayCheck(rateForShift(shift), hours);
double overtime = overtimePay(hours, rate);
double retirement = retirementDeduction(paycheck);
double totalPay = 0 ;
double overtimePlusPaycheck = paycheck + overtime;
if (shift == 1) {
System.out.println("You worked the 1st shift this week");
} else {
System.out.println("Do you participate in the retirement program? 1 for yes, 2 for no.");
int retirementProgram = keyboard.nextInt();
if (retirementProgram == 1) {
System.out.println("You particippate in the retirement program, your paycheck will be deducted.");
totalPay = paycheck - retirement + overtime;
} else {
System.out.println("You don't participate in the retirement program, your check did not get deducted");
totalPay = paycheck + overtime;
}
}
System.out.println("1. Hours worked " + hours + " hours");
System.out.println("2. You worked the " + shift + " shift");
System.out.println("3. Hourly Pay Rate = $" + rate);
System.out.println("4. Regular pay = $" + paycheck);
System.out.println("5. Overtime Pay = $" + overtime);
System.out.println("6. Total of regular and overtime pay = $ " + overtimePlusPaycheck);
System.out.println("7. Retirement deduction = $" + retirement);
System.out.println("8. Net Pay = $" + totalPay);
}
private static double calculatePayCheck(double rate, double hours) {
if (hours <= 40) {
return hours * rate;
} else {
return (40 * rate) + ((hours - 40) * (rate * 1.5));
}
}
private static double rateForShift(int shift) {
if (shift == 1) {
return 17;
} else if (shift == 2) {
return 18.50;
} else if (shift == 3) {
return 22;
}
return 0;
}
private static double retirementDeduction(double paycheck) {
double retirement = paycheck * .03;
return retirement;
}
private static double overtimePay(double hours, double rate) {
if (hours >= 40) {
return (hours - 40) * (rate * 1.5);
} else {
return 0;
}
}
}