简体   繁体   English

如何使用NetBeans中另一种方法的用户输入?

[英]How do I use a user input from another method in netbeans?

I'm trying to get the user input from the method Salary so I can use it in TaxCheck. 我正在尝试从方法Salary中获取用户输入,以便可以在TaxCheck中使用它。 Also if possible I would also like to add a new value named total salary which is the equivalent of 另外,如果可能的话,我还想添加一个新的值,称为总薪水,它等于

input user - corresponding deduction = total salary 输入用户-相应的扣减=总薪水

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");
    }
}

You pass the input/variable into another function by sending it as a argument. 您可以通过将输入/变量作为参数发送给另一个函数。

See this line of code 看到这行代码

 TaxCheck(inputS);

After sending it, you receive it into the other function by this line of code 发送后,您将通过此代码行将其接收到其他函数中

 TaxCheck(Double salary)

In that function, the first input will be accessible by the variable name salary. 在该函数中,第一个输入将通过变量名sal进行访问。

 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);
   }

You pass the input of one function to another function as an argument. 您将一个函数的输入作为参数传递给另一函数。 I'd suggest you to learn about passing arguments from one function to another in java. 我建议您学习在Java中将参数从一个函数传递给另一个函数的知识。 Hope i helped. 希望我能帮上忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM