如何使用参数将非常量变量值传递给方法?

huangapple go评论92阅读模式
英文:

How can I use parameters to pass down a non constant variable value to a method?

问题

我在努力将用户输入的变量值传递到我的程序中的一个方法中。我认为技术术语是参数。

我遇到的问题是,在用户在getnum()方法中输入数字后,我希望将这个数字传递给calculationcalculation_two这两个方法。然而,我似乎无法做到这一点。

只是为了解释一下这个程序,用户在getnum()方法中输入一个数字,然后他们进入选项方法,在选择选项后,在计算方法中需要传递在getnum()方法中输入的数字。因此,我将能够以这种方式进行计算。我也需要出于自己的个人原因设置程序。

有人可以帮忙吗?

谢谢

public static void main(String[] args) {

    getnum();
}

public static void getnum() {
    Scanner input = new Scanner(System.in);
    System.out.println("输入一个数字:");
    int num = input.nextInt();
    option(num);
}

public static void option(int num) {
    Scanner input2 = new Scanner(System.in);
    System.out.println("你想查看选项1还是2");
    int num2 = input2.nextInt();
    if (num2 == 1) {
        calculation(num);
    } else if (num2 == 2) {
        calculation_two(num);
    } else {
        System.exit(0);
    }

}

public static void calculation(int num) {
    // 在这里使用num进行计算
}

public static void calculation_two(int num) {
    // 在这里使用num进行另一种计算
}

这是修改后的代码,现在参数numgetnum()方法中获得,并传递给option()方法,然后再传递给calculation()calculation_two()方法,以便进行计算。

英文:

I'm struggling with passing a variable value that a user has entered into my program into a method. I think the technical word is parameters.

The problem I'm having is that after the user enters a number in the getnum() method I want the number to be passed down to the two methods calculation and `calculation_two. However, I can't seem to be able to achieve it.

Just to explain the program, the user enters a number in the getnum() method, then they go to the option method and after they select what option, in the calculations methods the number that was written in the getnum() method needs to be passed down the the calculations methods. Therefore, I will then be able to perform calculations with it that way. I need the program to be set up like this as well for my own personal reasons.

Can anyone assist please?

Thanks

public static void main (String[]args){

    getnum();
}

public static void getnum() {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int num = input.nextInt();
    option();
}

public static void option() {
    Scanner input2 = new Scanner(System.in);
    System.out.println("would you like to see option 1 or 2");
    int num2 = input2.nextInt();
    if(num2==1) {calculation();}
    else if(num2==2) {calculation_two();}
    else {System.exit(0);}

}

public static void calculation() {
}
public static void calculation_two() {
}

答案1

得分: 1

Declare your methods to use parameters and return values. Easiest way to do it:

public static void main (String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int num = input.nextInt();
    System.out.println("Would you like to see option 1 or 2?");
    int option = input.nextInt();
    int result = 0;
    if (option == 1) {
       result = calculation(num);
    } else if (option == 2) {
       result = calculation_two(num)
    } else {
       System.out.println("Invalid option, exiting...");
       System.exit(0);
    }
    System.out.println("Result = " + result);
}

private static int calculation(int num) {
    // implement this
    return 0;
}
private static int calculation_two(int num) {
    // implement that
    return 0;
}
英文:

Declare your methods to use parameters and return values. Easiest way to do it:

public static void main (String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    int num = input.nextInt();
    System.out.println("would you like to see option 1 or 2");
    int option = input.nextInt();
    int result = 0;
    if (option == 1) {
       result = calculation(num);
    } else if (option == 2) {
       result = calculation_two(num)
    } else {
       System.out.println("Invalid option, exiting...");
       System.exit(0);
    }
    System.out.println("Result = " + result);
}

private static int calculation(int num) {
    // implement this
    return 0;
}
private static int calculation_two(int num) {
    // implement that
    return 0;
}

答案2

得分: 1

请参考按引用传递和按值传递以进一步澄清原始类型或对象如何从一个方法传递到另一个方法。

以下是从一个方法传递参数到另一个方法的步骤:

  1. 您将从扫描器获得的int作为参数传递给option方法调用:
    public static void getnum() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int num = input.nextInt();
        option(num);   // 将int作为参数传递
    }
  1. 现在,由于您正在调用带参数的方法,您需要更改option方法的签名以接受参数。您将int值(theNumber变量)从option方法的参数传递给calculation方法调用。

参数和参数是可以互换使用的术语,但您可以在这里查看它们的区别。

    public static void option(int theNumber) {   // option现在接受一个int参数
        Scanner input2 = new Scanner(System.in);
        System.out.println("would you like to see option 1 or 2");
        int num2 = input2.nextInt();
        if(num2==1) {   
           calculation(theNumber);  // 方法现在接受一个参数
        }
        else if(num2==2) {
           calculation_two(theNumber);  // 方法现在接受一个参数
        }
        else {System.exit(0);}
    }
  1. 您再次将参数传递给calculation(s)方法并更改签名为:
    public static void calculation(int theNumber) {  // 带参数的方法
    
    }
    
    public static void calculation_two(int theNumber) {
    
    }
英文:

Please see call by reference and call by value to further clarify how primitives or objects are passed from one method to another.

Here are the steps to pass the parameter from one method to another:

  1. You pass the int you got from the scanner as an argument in the option method call:
    public static void getnum() {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int num = input.nextInt();
        option(num);   // You pass the int as an argument
    }
  1. Now, since you are now calling a method with arguments you need to change the signature of the option method to take a parameter. You pass the int value(theNumber variable) from the option method's parameter to the calculation method call.

Arguments and parameters are terms which are used interchangeably but you can check the difference here.

    public static void option(int theNumber) {   // option now takes a int parameter
        Scanner input2 = new Scanner(System.in);
        System.out.println("would you like to see option 1 or 2");
        int num2 = input2.nextInt();
        if(num2==1) {   
           calculation(theNumber);  // method now takes an argument
        }
        else if(num2==2) {
           calculation_two(theNumber);  // method now takes an argument
        }
        else {System.exit(0);}
    }
  1. You pass the parameter again to the calculation(s) method and change the signature to:
    public static void calculation(int theNumber) {  // method with parameter
    
    }
    
    public static void calculation_two(int theNumber) {
    
    }

答案3

得分: -1

package in.faridabad.mandheer;

public class Main{

    public static void main (String[]args){
        Main m = new Main();
        // 假设计算是 "sum"
        System.out.println("sum is + " + m.option());
    }

    private int getnum() {
        Scanner input = new Scanner(System.in);
        return input.nextInt();
    }

    int option() {
        System.out.println("你想看选项 1 还是 2");
        int num2 = getnum();
        if(num2==1) {return calculation();}
        else if(num2==2) {return calculation_two();}
        else {System.exit(0);}
    }

    private int calculation() {
        System.out.println("输入一个数字:");
        int num1 = getnum();
        return num1;
    }

    private int calculation_two() {
        System.out.println("输入一个数字:");
        int num1 = getnum();
        System.out.println("输入第二个数字:");
        int num2 = getnum();
        return num1 + num2;
    }
}
英文:

To answer with secure coding practices

package in.faridabad.mandheer;

public class Main{

    public static void main (String[]args){
        Main m = new Main();
        // Assuming calculation is "sum"
        System.out.println("sum is + "+ m.option());
    }

    private int getnum() {
        Scanner input = new Scanner(System.in);
        return input.nextInt();
    }

    int option() {
        System.out.println("would you like to see option 1 or 2");
        int num2 = getnum();
        if(num2==1) {return calculation();}
        else if(num2==2) {return calculation_two();}
        else {System.exit(0);}
    
    }

    private int calculation() {
        System.out.println("Enter a number: ");
        int num1 = getnum();
        return num1;
    }

    private int calculation_two() {
        System.out.println("Enter a number: ");
        int num1 = getnum();
        System.out.println("Enter 2nd number: ");
        int num2 = getnum();
        return num1+num2;
    }
}

huangapple
  • 本文由 发表于 2020年7月23日 00:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63038799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定