这是我的一道自学作业题。
我应该在不实际运行该程序的情况下写下该程序的输出。 我了解这里的所有语法和变量传递(并且我有答案)但出于某种原因,在纸上追踪它是行不通的。
是否有某种 secret 的图表技术可以使一切井井有条?
对于有经验的程序员来说,手工解决这个问题是否只是儿戏?
谢谢
public class Mystery extends ConsoleProgram {
public void run() {
ghost(13);
}
private void ghost(int x) {
int y = 0;
for (int i = 1; i < x; i *= 2) {
y = witch(y, skeleton(x, i));
}
println("ghost: x = " + x + ", y = " + y);
}
private int witch(int x, int y) {
x = 10 * x + y;
println("witch: x = " + x + ", y = " + y);
return x;
}
private int skeleton(int x, int y) {
return x / y % 2;
}
}
最佳答案
尝试用传入的数字写下一堆方法调用,例如:
run()
ghost(13)
skeleton(13,1)
等等
在每次方法调用时,找到一些暂存空间并写出变量,并尝试计算出代码的作用,直到获得返回值。然后获取该返回值并返回到该方法堆栈中的最后一点并将该方法划掉。
例如,上面示例堆栈的顶部(底部)是 skeleton(13,1)
,因此您尝试找出 skeleton()
返回什么x 是 13,y 是 1。很简单,它是 1
。然后回到堆栈,看看返回值应该去哪里。在本例中是 witch()
,所以堆栈是:
run()
ghost(13)
witch(0,1)
并继续直到 run() 完成。
关于java - 在跟踪此程序时,我可以使用哪种图表方法来保持一切井井有条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11814096/