java - 如何让我的扣除仅在用户选择时才起作用?

标签 java

我的程序中的所有内容都正常,但我只希望在用户属于该程序的情况下运行退休扣除。现在,无论如何它都在运行。我该如何制作它,以便仅当用户在第二类或第三类工作时输入“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;
        }
    }
}

最佳答案

在您的方法中使用 boolean 标志:

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;  // Set flag to true.
    }
    else {
        System.out.println("You don't participate in the retirement program, "
                         + "your check did not get deducted.");
    }
}

然后在哪里声明并初始化 double 型退休变量:

/* Ternary Operator used here. Same as doing:
      double retirement = 0.0d;  
      if (inRetirementProgram) { retirementDeduction(paycheck); }
*/
double retirement = (inRetirementProgram ? retirementDeduction(paycheck) : 0.0d);

关于java - 如何让我的扣除仅在用户选择时才起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61046382/

相关文章:

java - 嵌套循环说明 - 已编辑

java - 在 @Async 函数完成之前返回值

Java JTextPane 斜体帮助

java - 渲染旋转图像

java - 按属性在列表中查找特定对象

java - 为什么这个对象在此 ArrayList 中显示为 "unknown class"?

java - Java中删除文件中的特定记录

java - Dockerized Mac/Java 应用程序无法与本地主机通信

java - 包含其自身的 ArrayList 的 Parcelable

java - 从文件 java 中收集元数据