这是过去的试卷中的一个问题.代码在问题中给出,我需要获得值和断点的次数.我试过在eclipse中运行代码但无济于事.(如果代码执行,我可以在调试模式下找到值)
此问题还指出:该方法fact在类的实例上调用,其中n值为6.不确定我做错了什么,因为代码与问题中给出的完全相同.
public class FactLoop {
private int n;// assumed to be greater than or equal to 0
/**
* Calculate factorial of n
*
* @return n!
*/
public int fact() {
int i = 0;
int f = 1;
/**
* loop invariant 0<=i<=n and f=i!
*/
while (i < n) {// loop test (breakpoint on this line)
i = i++;
f = f * i;
}
return f;
}
// this main method is not a part of the given question
public static void main(String[] args) {
FactLoop fl = new FactLoop();
fl.n = 6;
System.out.println(fl.fact());
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |