In Java, how can I take a variable obtained from a user input from one method and use the output in another method?

大兔子大兔子 提交于 2019-12-08 05:06:55

问题


I can't figure out how to bring a variable from one method into another for use, especially that from a user input. For example, this test program doesn't work. How would I make it work?

 
import java.util.*;

public class Test {
    public static void main(String[] args) {
    input();
    output();   
   }

    public static void input() {

        Scanner console = new Scanner(System.in);

        System.out.print("This number multiplied by 7: ");
        int number = console.nextInt();

        int number7 = number * 7;
        System.out.print("The result is: " + number7);

    }
    public static void output() {

        Scanner console = new Scanner(System.in);

        System.out.print("The result multiplied by two: ");
        int number = console.nextInt();

        int number2 = number7 * 2;
        System.out.print("The result is: " + number2);
    }
}

回答1:


There are (at least) two ways. One is to define your input() method to return the value needed in the output() method:

import java.util.*;

public class Test {
    public static void main(String[] args) {
    int in = input();
    output(in);   
   }

    public static int input() {

        Scanner console = new Scanner(System.in);

        System.out.print("This number multiplied by 7: ");
        int number = console.nextInt();

        int number7 = number * 7;
        System.out.print("The result is: " + number7);
        return number7;
    }

    public static void output(int number7) {

        Scanner console = new Scanner(System.in);

        System.out.print("The result multiplied by two: ");
        int number = console.nextInt();

        int number2 = number7 * 2;
        System.out.print("The result is: " + number2);
    }
}

The other is to declare a class variable:

import java.util.*;

public class Test {
    static int number7;
    public static void main(String[] args) {
    input();
    output();   
   }

    public static void input() {

        Scanner console = new Scanner(System.in);

        System.out.print("This number multiplied by 7: ");
        int number = console.nextInt();

        int number7 = number * 7;
        System.out.print("The result is: " + number7);
    }

    public static void output() {

        Scanner console = new Scanner(System.in);

        System.out.print("The result multiplied by two: ");
        int number = console.nextInt();

        int number2 = number7 * 2;
        System.out.print("The result is: " + number2);
    }
}



回答2:


A function with a name like input should not be returning void. And a function with a name like output should be taking as an argument the material to be output (or compute the output from).

import java.util.*;

public class Test {
    public static void main(String[] args) {
    final int myInput = input();
    output(myInput);   
   }

    public static int input() { // not void

        Scanner console = new Scanner(System.in);

        System.out.print("This number multiplied by 7: ");
        int number = console.nextInt();

        int number7 = number * 7;
        System.out.print("The result is: " + number7);
        return number7;

    }
    public static void output(int input) {

      //  Scanner console = new Scanner(System.in); 
     // NO, you don't want to be getting INPUT now! You are making output!

        System.out.print("The result multiplied by two: ");
     //   int number = console.nextInt();

        int number2 = input * 2; // not number7, it lives only in the input routine
        System.out.print("The result is: " + number2);
    }
}

You should go back and review the concept of methods.




回答3:


You can pass variable as an argument to another method.

A basic example:-

private void method1() {
        int i = 10;
        method2(i);
    }

    private void method2(int i) {

    }

Example if you wanna pass console in output method then you should do something like this:

public class Test {

    public static void main(String[] args) {
    int number7;
    Scanner console = input();
    output(console);   
   }

    public static Scanner input() {

        Scanner console = new Scanner(System.in);

        System.out.print("This number multiplied by 7: ");
        int number = console.nextInt();

        number7 = number * 7;
        System.out.print("The result is: " + number7);
        return console;

    }
    public static void output(Scanner console) {

        System.out.print("The result multiplied by two: ");
        int number = console.nextInt();

        int number2 = number7 * 2;
        System.out.print("The result is: " + number2);
    }
}



回答4:


Java method have a return type and parameter list in parenthesis, so in your case instead of returning void return some datatype to be used in other method.




回答5:


Your input and output methods are not doing what their name suggests. They are both reading input, calculating results and printing output.

Here's different design, showing how you can separate the concerns and pass values:

import java.util.Scanner;

public class MultiplyBySevenMachine {

public static void main(String[] args) {

    MultiplyBySevenMachine multiplyBySevenMachine = new MultiplyBySevenMachine();
    multiplyBySevenMachine.run();
}

private void run() {
    while (true) {
        int input = readInput();
        int result = calculateResult(input);
        writeOutput(result);
    }
}

private int readInput() {
    Scanner console = new Scanner(System.in);
    System.out.print("Please type a number: ");
    int number = console.nextInt();
    return number;
}

private int calculateResult(int input) {
    int result = input * 7;
    return result;
}

private void writeOutput(int result) {
    System.out.println("The result is: " + result);
}
}



回答6:


Another mistake to point out is that you never close your scanner and only thing that saves you from the fail compilation is your JVM that corrects this mistake. Just imagine if any developer has an old JVM at it doesn't have this intuitive functionality to correct your mistakes.

    import java.util.*;
    public class Test {
        public static void main(String[] args) {
        input();
        output();   
    }

    public static void input() {

        Scanner console = new Scanner(System.in);

        System.out.print("This number multiplied by 7: ");
        int number = console.nextInt();

        int number7 = number * 7;
        System.out.print("The result is: " + number7);
        console.close(); // This is important!!!

    }
    public static void output() {

        Scanner console = new Scanner(System.in);

        System.out.print("The result multiplied by two: ");
        int number = console.nextInt();

        int number2 = number7 * 2;
        System.out.print("The result is: " + number2);
        console.close(); // This is important!!!
    }
}



回答7:


You need to make the variables used in the methods class variables. You would do that by putting static in front of it. Static means that any class or method can use it. If you forget to use static, you will have yourself what is called a local variable, in which is only accessible through the method it was created in. Also, in that program, you made two scanner variables. You could have just made one and put static in front of it. I hope this answer to your question helps.



来源:https://stackoverflow.com/questions/5769024/in-java-how-can-i-take-a-variable-obtained-from-a-user-input-from-one-method-an

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!