如何使我的扣除只在用户选择时起作用?

huangapple 未分类评论61阅读模式
英文:

How do I get my deduction to work only if the user chooses?

问题

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 participate 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;
        }
    }
}
英文:

Everything in my program is working but I only want the retirement deduction to run if the user is a part of that program. Right now, it's running regardless. How would I make it so it only runs if the user enters a "1" while they work the 2nd or 3rd shift and not run if they choose a "2". I've tried changing the ordering of my if statement and including my method call in it but that ended up giving me a ton of errors. I think my ordering is wrong?

import java.util.*;

public class AcmePay {
    public static void main(String[] args) throws Exception {
        Scanner keyboard = new Scanner(System.in);

        System.out.println(&quot;How many hours did you work this week?&quot;);
        double hours = keyboard.nextDouble();

        System.out.println(&quot;What shift did you work? 1, 2, or 3?&quot;);
        int shift = keyboard.nextInt();
        if (shift == 1) {
            System.out.println(&quot;You worked the 1st shift this week&quot;);
        }
        else {
            System.out.println(&quot;Do you participate in the retirement program? 1 for yes, 2 for no.&quot;);
            int retirementProgram = keyboard.nextInt();
            if (retirementProgram == 1) {
                System.out.println(&quot;You particippate in the retirement program, your paycheck will be deducted.&quot;);
            }
            else {
                System.out.println(&quot;You don&#39;t participate in the retirement program, your check did not get deducted&quot;);
            }
        }

        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(&quot;1. Hours worked &quot; + hours + &quot; hours&quot;);
        System.out.println(&quot;2. You worked the &quot; + shift + &quot; shift&quot;);
        System.out.println(&quot;3. Hourly Pay Rate = $&quot; + rate);
        System.out.println(&quot;4. Regular pay = $&quot; + paycheck);
        System.out.println(&quot;5. Overtime Pay = $&quot; + overtime);
        System.out.println(&quot;6. Total of regular and overtime pay = $ &quot; + overtimePlusPaycheck);
        System.out.println(&quot;7. Retirement deduction = $&quot; + retirement);

        System.out.println(&quot;8. Net Pay = $&quot; + totalPay);
    }

    private static double calculatePayCheck(double rate, double hours) {
        if (hours &lt;= 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 &gt;= 40) {
            return (hours - 40) * (rate * 1.5);
        }
        else {
            return 0;
        }
    }
}

答案1

得分: 0

使用一个布尔标志在你的方法中:

public static void main(String[] args) throws Exception {
    Scanner keyboard = new Scanner(System.in);
    boolean inRetirementProgram = false;  // 标志:默认为 false。
    // .... 你的代码的其余部分 ...
}

然后在被问及是否参加退休计划时:

else {
    System.out.println("你是否参加退休计划?输入 1 表示是,输入 2 表示否。");
    int retirementProgram = keyboard.nextInt();
    if (retirementProgram == 1) {
        System.out.println("你参加了退休计划,你的薪水将会被扣除。");
        inRetirementProgram = true;  // 将标志设置为 true。
    }
    else {
        System.out.println("你未参加退休计划,你的薪水未被扣除。");
    }
}

然后在声明和初始化双精度类型的 retirement 变量的地方:

/* 这里使用了三元运算符。与以下代码等效:
      double retirement = 0.0d;  
      if (inRetirementProgram) { retirementDeduction(paycheck); }
*/
double retirement = (inRetirementProgram ? retirementDeduction(paycheck) : 0.0d);
英文:

Use a boolean flag in your method:

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 ...
}

Then when asked if in the retirement program:

else {
    System.out.println(&quot;Do you participate in the retirement program? 1 for yes, 2 for no.&quot;);
    int retirementProgram = keyboard.nextInt();
    if (retirementProgram == 1) {
        System.out.println(&quot;You particippate in the retirement program, &quot;
                + &quot;your paycheck will be deducted.&quot;);
        inRetirementProgram = true;  // Set flag to true.
    }
    else {
        System.out.println(&quot;You don&#39;t participate in the retirement program, &quot;
                         + &quot;your check did not get deducted.&quot;);
    }
}

Then where you declare and initialize the double type retirement variable:

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

huangapple
  • 本文由 发表于 2020年4月6日 01:11:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61046382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定