【发布时间】:2016-03-11 03:38:12
【问题描述】:
我正在学习 Java,发现一点点知识都令人困惑。 目标是编写一个等效于 n! 的方法。功能。我正在使用 for 循环来乘以在方法外部声明的变量。我得到的只是 0。
我做错了什么?
//
// Complete the method to return the product of
// all the numbers 1 to the parameter n (inclusive)
// @ param n
// @ return n!
public class MathUtil
{
public int total;
public int product(int n)
{
for (int i = 1; i == n; i ++)
{
total = total * i;
}
return total;
}
}
【问题讨论】:
-
阅读这行代码并告诉我它有什么问题:
for (int i = 1; i == n; i ++)提示:初始化、条件、增量。 -
Total 未用值初始化。
-
@kirbyquerby 无关
-
@Kon。不,不是,lmao。
-
请学习一些基本的调试技巧。学习使用调试器,或者至少在一些有意义的位置打印出信息。您应该能够自己轻松解决问题