Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用户输入与变量相关?_Java_Variables_Input_User Input - Fatal编程技术网

Java 如何使用户输入与变量相关?

Java 如何使用户输入与变量相关?,java,variables,input,user-input,Java,Variables,Input,User Input,我不知道如何准确地表达这个问题,但这正是我想要实现的 (我正在使用堆栈实现河内塔楼插图: 这在main()函数中: System.out.println("Type the source pole number and the destination pole number"); int a = reader.nextInt(); int b = reader.nextInt(); boolean move = moveDics(a, b); 这些是表示3个极点的堆栈: Stack&l

我不知道如何准确地表达这个问题,但这正是我想要实现的 (我正在使用堆栈实现河内塔楼插图:

这在
main()
函数中:

System.out.println("Type the source pole number and the destination pole number");
int a = reader.nextInt();
int b = reader.nextInt();
boolean move = moveDics(a, b);
这些是表示3个极点的堆栈:

    Stack<Integer> pole1 = new Stack<Integer>();
    Stack<Integer> pole2 = new Stack<Integer>();
    Stack<Integer> pole3 = new Stack<Integer>();
Stack pole1=新堆栈();
堆栈pole2=新堆栈();
堆栈pole3=新堆栈();
我想根据用户输入更改堆栈,为此,我需要关联变量pole1、pole2、pole3(执行任何操作,如
pole1.pop()


这就是我的问题:除了多个if()语句或switch case语句之外,我如何使用用户输入-一个整数-来关联极点?类似于
pole+“x”.pop()

好的解决方案

不要制造太多这样的变量

您可以将它们全部放在一个数组中:

Stack[] poles = new Stack[3];
for (int i=0; i<poles.length; i++) poles[i] = new Stack<Integer>();
然后可以使用
极点访问极点。获取(yourInteger)

注意,一旦你开始在这些极点上做更复杂的事情,你就必须考虑将它们嵌入到一个类中。我个人试图避免集合或集合的集合,因为它们往往会混淆。

不是很好的解决方案

您可以使用开关:

public Stack<Integer> getPole(int i) {
    switch(myInteger) {
    case 1:
        return pole1;
    case 2:
        return pole2;
    case 3:
        return pole3
    }
    return null;
}

然后,您必须获取此字段值的方法,并调用它们。这将是一个缓慢的过程,许多LOC和许多try/catch。

好的解决方案

不要制造太多这样的变量

您可以将它们全部放在一个数组中:

Stack[] poles = new Stack[3];
for (int i=0; i<poles.length; i++) poles[i] = new Stack<Integer>();
然后可以使用
极点访问极点。获取(yourInteger)

注意,一旦你开始在这些极点上做更复杂的事情,你就必须考虑将它们嵌入到一个类中。我个人试图避免集合或集合的集合,因为它们往往会混淆。

不是很好的解决方案

您可以使用开关:

public Stack<Integer> getPole(int i) {
    switch(myInteger) {
    case 1:
        return pole1;
    case 2:
        return pole2;
    case 3:
        return pole3
    }
    return null;
}

然后你必须得到这个字段值的方法,并调用它们。这会很慢,很多LOC和很多try/catch。

谢谢你的快速回答,我应该知道使用堆栈数组实现它要容易得多。不管怎样,有什么方法可以实现我想做的吗?@dystroy你不能创建通用数组。试试
列表
@amiregelz是的,使用反射可以很容易地处理各种变量,也许我会将代码添加到我的答案中,但事实上,不要这样做!@amiregelz我添加了其他解决方案,但只有第一个解决方案会被理智和有经验的java程序员使用。@amiregelz列表是一个接口,您必须使用具体的实现类似这样的提示:
List List=new ArrayList()
谢谢你的快速回答,我本应该知道使用stacks数组实现它要容易得多。不管怎样,有什么方法可以实现我想做的吗?@dystroy你不能创建泛型数组。试试
列表
@amiregelz是的,使用反射,也许我可以用你想要的多样变量来实现它“我会将代码添加到我的答案中,但实际上,不要这样做!”@amiregelz我添加了其他解决方案,但只有第一个解决方案会被一个理智且经验丰富的java程序员使用。@amiregelz List是一个接口,您必须使用如下具体实现:
List=new ArrayList();
Field stackField = MyClass.class.getField("pole"+myInteger);