如何在netbeans中使用其他方法的用户输入?

时间:2018-01-24 15:17:12

标签: java methods

我正在尝试从方法Salary获取用户输入,因此我可以在TaxCheck中使用它。 如果可能的话,我还想添加一个名为总薪水的新值,相当于

输入用户 - 相应扣除=总薪水

public static double Salary() throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.print("Input a salary: ");
    double inputS = Double.parseDouble(br.readLine());
    return inputS;
}

public static void TaxCheck() throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    double inputS = Salary();
    double salary = Double.parseDouble(br.readLine());
    double salary1 = 15000.00;
    double salary2 = 20000.00;
    double salary3 = 25000.00;
    double salary4 = 30000.00;
    double salary5 = 35000.00;
    double deduction3 =  634.57;
    double deduction4 = 1624.57;
    double deduction5 = 2655.72;
    if (inputS>=salary5) {
        System.out.println("Rate Deduction: "+deduction5+"");
    }
    if (inputS>=salary4) {
        System.out.println("Rate Deduction: "+deduction4+"");
    }
    if (inputS>=salary3) {
        System.out.println("Rate Deduction: "+deduction3+"");
    } else {
        System.out.println("This salary does not have a deduction");
    }
}

1 个答案:

答案 0 :(得分:0)

通过将输入/变量作为参数发送,将输入/变量传递给另一个函数。

查看此行代码

 TaxCheck(inputS);

发送后,您可以通过此行代码将其接收到另一个函数中

 TaxCheck(Double salary)

在该函数中,第一个输入可以通过变量名称salary来访问。

 public static void main(String[] args) {
            try{
             Salary();
            }catch(IOException e){
               e.printStackTrace();
            }
        }



public static void Salary()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
        System.out.print("Input a salary: ");
        double inputS = Double.parseDouble(br.readLine());
        TaxCheck(inputS);
}


private static void TaxCheck(Double salary)throws IOException{
        double totalSalary = salary;
        double salary1 = 15000.00;
        double salary2 = 20000.00;
        double salary3 = 25000.00;
        double salary4 = 30000.00;
        double salary5 = 35000.00;
        double deduction3 =  634.57;
        double deduction4 = 1624.57;
        double deduction5 = 2655.72;
        if (salary>=salary5){
            System.out.println("Rate Deduction: "+deduction5+"");
            totalSalary = totalSalary - deduction5;
        }
        if (salary>=salary4){
            System.out.println("Rate Deduction: "+deduction4+"");
            totalSalary = totalSalary - deduction4;
        }
        if (salary>=salary3){
            System.out.println("Rate Deduction: "+deduction3+"");
            totalSalary = totalSalary - deduction3;
        }
        else {
            System.out.println("This salary does not have a deduction");
        }
        System.out.println("Total Salary = "+totalSalary);
   }

将一个函数的输入作为参数传递给另一个函数。我建议你学习如何在java中将参数从一个函数传递给另一个函数。希望我能帮忙。