gpt4 book ai didi

Java - 在此方法上使用断点

转载 作者:行者123 更新时间:2023-11-29 10:18:18 26 4
gpt4 key购买 nike

这是过去试卷中的一道题。问题中给出了代码,我需要获取值以及断点是多少次。我试过在 eclipse 中运行代码但无济于事。 (如果执行代码,我可以在 Debug模式下找到这些值)

问题还指出:方法 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());
}

}

最佳答案

您的错误在 i=i++; 中。 i++ 递增 i,并返回 i 的旧值。通过说 i=i++,您递增它,然后将它设置为它的旧值。

只需使用 i++; 来增加它。

关于Java - 在此方法上使用断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12112605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com