java—在此方法上使用断点

7bsow1i6  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(306)

这是一道过去试卷上的题。问题中给出了代码,我需要获得值以及断点的次数。我尝试过在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());
}
``` `}` 
fivyi3re

fivyi3re1#

你的错误在 i=i++; . i++ 增加i,并重新运行i的旧值。通过说 i=i++ ,将其递增,然后将其设置为旧值。
只是使用 i++; 增加它。

相关问题