英文:
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("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;
}
}
}
答案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("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.");
}
}
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);
专注分享java语言的经验与见解,让所有开发者获益!
评论