在Java中,如何从一个方法获取从用户输入获得的变量并在另一个方法中使用输出?

时间:2011-04-24 06:20:18

标签: java methods

我无法弄清楚如何将一个变量从一个方法转移到另一个方法中,尤其是来自用户输入的方法。例如,此测试程序不起作用。我怎么能让它发挥作用?

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

7 个答案:

答案 0 :(得分:3)

有(至少)两种方式。一种是定义input()方法以返回output()方法中所需的值:

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

另一种是声明一个类变量:

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

答案 1 :(得分:1)

您可以将变量作为参数传递给另一个方法。

一个基本的例子: -

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

    private void method2(int i) {

    }

示例如果您想在console方法中传递output,那么您应该执行以下操作:

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

答案 2 :(得分:1)

具有输入名称的函数不应返回void。并且具有类似输出的名称的函数应该将要输出的材料作为参数(或计算输出)。

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

您应该返回并查看方法的概念。

答案 3 :(得分:0)

Java方法在括号中有一个返回类型和参数列表,因此在你的情况下,而不是返回void返回一些数据类型,以便在其他方法中使用。

答案 4 :(得分:0)

您的输入和输出方法没有按照他们的名字所暗示的那样进行。它们都是读取输入,计算结果和打印输出。

这是不同的设计,展示了如何分离关注点和传递值:

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

答案 5 :(得分:0)

要指出的另一个错误是你永远不会关闭你的扫描仪,只有那些能让你免于失败编译的东西就是你的JVM纠正了这个错误。试想一下,如果任何开发人员都有一个旧的JVM,它就没有这种直观的功能来纠正你的错误。

    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!!!
    }
}

答案 6 :(得分:-1)

您需要在方法类变量中使用变量。你可以通过在它前面放置静态来做到这一点。静态意味着任何类或方法都可以使用它。如果您忘记使用静态,您将获得所谓的局部变量,其中只能通过创建它的方法访问。此外,在该程序中,您创建了两个扫描变量。你可以刚制作一个并在其前面放置静电。我希望这个问题的答案有所帮助。