Java - 在此方法上使用断点

nsc*_*010 1 java breakpoints

这是过去的试卷中的一个问题.代码在问题中给出,我需要获得值和断点的次数.我试过在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)

}

Ale*_*man 8

你的错误是在i=i++;.i++增加i,并重新调整我的旧值.通过说i=i++,你增加它,然后将其设置为旧值.

只是用i++;它来增加它.