我不知道如何精确短语这个问题,但是这是我想达到 (我用栈实现河内说明的塔是什么:如何使用户输入与变量相关?
这是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>();
我想改变基于用户输入的堆栈,并这样做,我需要相关的变量pole1,pole2,pole3(预制任何动作,如pole1.pop()
)。
这是我的问题:除了多个if()语句或switch case语句之外,我怎样才能将用户输入 - 一个整数 - 与极点相关?像pole + "x".pop()
?
感谢您的快速回答,我应该想出使用堆栈数组来实现它更容易。无论如何,有没有办法实现我想要做的事? – amiregelz 2012-07-19 12:28:43
@dystroy您不能制作通用数组。尝试'List>'。 –
Jeffrey
2012-07-19 12:29:35
@amiregelz是的,使用反射可以很容易地完成你想要的各种变量,也许我会将代码添加到我的答案中,但实际上,**不会!**。 – 2012-07-19 12:32:30