在NetBeans中如何跨方法复用用户输入?实现薪资扣税与总薪资计算
Solution for Reusing Salary Input and Adding Total Salary Calculation
Hey there, let's fix up your code to meet your requirements properly. First, let's break down the issues in your original code:
- Your
TaxCheckmethod was creating a newBufferedReaderand reading input again, which was redundant since you already have theSalarymethod for that. - The multiple
ifstatements withoutelsewould cause multiple deduction messages to print when a salary meets multiple thresholds (e.g., a salary of 35000 would trigger all three upper condition checks). - There was no logic to calculate and display the total salary after deductions.
Here's the revised code that addresses all these points:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class SalaryTaxCalculator { // Single responsibility: handle user input for salary public static double getSalary() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Input a salary: "); return Double.parseDouble(br.readLine()); } // Reuse the salary input via parameter, handle tax deductions and total salary calculation public static void taxCheck(double salary) { // Define threshold and deduction constants (following Java naming conventions) final double THRESHOLD_35K = 35000.00; final double THRESHOLD_30K = 30000.00; final double THRESHOLD_25K = 25000.00; final double DEDUCTION_35K = 2655.72; final double DEDUCTION_30K = 1624.57; final double DEDUCTION_25K = 634.57; double applicableDeduction = 0.0; // Use else-if to ensure only one condition is matched if (salary >= THRESHOLD_35K) { applicableDeduction = DEDUCTION_35K; System.out.println("Rate Deduction: " + applicableDeduction); } else if (salary >= THRESHOLD_30K) { applicableDeduction = DEDUCTION_30K; System.out.println("Rate Deduction: " + applicableDeduction); } else if (salary >= THRESHOLD_25K) { applicableDeduction = DEDUCTION_25K; System.out.println("Rate Deduction: " + applicableDeduction); } else { System.out.println("This salary does not have a deduction"); } // Calculate and display total salary (input salary - deduction) double totalSalary = salary - applicableDeduction; System.out.printf("Total Salary after deduction: %.2f%n", totalSalary); } // Main method to run the program public static void main(String[] args) throws IOException { double userInputSalary = getSalary(); taxCheck(userInputSalary); } }
Key Changes Explained:
- Reused Salary Input: I modified
taxCheckto accept adoubleparameter. In themainmethod, we first get the salary fromgetSalary()(renamed for clarity) and pass it totaxCheck—this way we only read user input once and reuse it. - Fixed Condition Logic: Switched from multiple standalone
ifstatements toelse-if, so only the highest applicable threshold is matched (no duplicate deduction messages). - Added Total Salary Calculation: Added a variable
applicableDeductionto track the deduction amount, then calculated the total salary using your formula (total salary = input salary - deduction) and printed it with formatted decimal places for readability. - Improved Naming: Renamed methods and variables to follow Java conventions (e.g.,
getSalaryinstead ofSalary, uppercase constants) to make the code easier to read and maintain.
内容的提问来源于stack exchange,提问作者Jonathan Kedawen Jr.
相关产品推荐
相关产品推荐

