You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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 TaxCheck method was creating a new BufferedReader and reading input again, which was redundant since you already have the Salary method for that.
  • The multiple if statements without else would 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 taxCheck to accept a double parameter. In the main method, we first get the salary from getSalary() (renamed for clarity) and pass it to taxCheck—this way we only read user input once and reuse it.
  • Fixed Condition Logic: Switched from multiple standalone if statements to else-if, so only the highest applicable threshold is matched (no duplicate deduction messages).
  • Added Total Salary Calculation: Added a variable applicableDeduction to 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., getSalary instead of Salary, uppercase constants) to make the code easier to read and maintain.

内容的提问来源于stack exchange,提问作者Jonathan Kedawen Jr.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:21:09