Java - 在此方法上使用断点

标签 java breakpoints

这是过去试卷中的一道题。问题中给出了代码,我需要获取值以及断点是多少次。我试过在 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/

相关文章:

java - 如何使用 JDBC 来最大化 Netty 的性能?

c# - 为什么我无法在 Visual Studio 2015 中调试 Silverlight?

debugging - 有条件地设置断点或对断点进行编码的任何方法吗?

java - 配置IntelliJ Java异常断点

java - 在 Java 中不可变

java web 应用程序缓存数据,如何停止它以使数据不陈旧!

java - 在 JDBC 中使用变量

PHP 的 pack 函数的 Java 等价物

Windbg:打印触发的断点号

debugging - Android Studio IDE : Break on Exception