各位,我有两个问题:
1`在跑完之后,tower2里面依然是空的,也就是说调用方程move
后,tower2没有被改变,我想知道问题出在哪了。
2`两个方程moveTopTo
和move
为什么必须是static?
在此先谢谢了!
import java.util.Stack;
public class ThreePFive {
public static void main (String[] args) {
int n = 3;
Stack<Integer> tower0 = new Stack<Integer>();
Stack<Integer> tower1 = new Stack<Integer>();
Stack<Integer> tower2 = new Stack<Integer>();
for (int i = n - 1; i >= 0; i--) {
tower0.push(i);
}
move(n, tower0, tower1, tower2);
}
public static void moveTopTo(Stack<Integer> ori, Stack<Integer> des) {
int top = ori.pop();
des.push(top);
}
public static void move(int n, Stack<Integer> ori, Stack<Integer> des, Stack<Integer> buf) {
if (n > 0) {
move(n - 1, ori, buf, des);
moveTopTo(ori, des);
move(n - 1, buf, des, ori);
}
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
tower1
的内容?有没有仔细思索move
的各参数的命名的含义?static
不过是避免使用对象实例罢了,本程序并没有使用任何对象,是纯粹的面对过程编程。