为什么阶乘法总是 return 相同的结果?
Why does factorial method always return the same result?
public static void main(String[] args) {
// Luke Mihalovich
Scanner keyboard = new Scanner(System.in);
int n;
System.out.print("Enter a non-negative integer (-1 to quit) : ");
n = keyboard.nextInt();
int factorial = Factorial(n);
while (n >= 1) {
System.out.print("Enter a non-negative integer (-1 to quit) : ");
n = keyboard.nextInt();
System.out.println(n + "! = " + factorial);}
if (n == 0) {
System.out.print(n = 1); }
if (n == -1) {
System.out.print("Goodbye!"); }
}
public static int Factorial(int n) {
int factorial = 1;
for(int i= 1;i<n;) {
i++;
factorial = factorial * i; }
return factorial;
}
}
此程序为所有输入打印相同的结果。
例如,如果我输入5
,正确答案是5! = 120
。但是如果我输入4
,它又打印了4! = 120
,这是错误的,应该是24
.
说明
您写道:
int factorial = Factorial(n);
while (n >= 1) {
// ...
System.out.println(n + "! = " + factorial);
}
没有更新 factorial
。像
这样的语句
int factorial = Factorial(n);
仅执行 一次 而不是 live-linked 以便在您更新 n
.
后重新计算
解决方案
因此您需要在每次迭代中显式更新 factorial
:
int factorial = Factorial(n);
while (n >= 1) {
// ...
factorial = Factorial(n);
System.out.println(n + "! = " + factorial);
}
备注
请遵守命名约定。方法名称应始终以 小写字母 字符开头,因此应改为 factorial
。然后变量应该是re-named,例如result
。
你的for-loop看起来很奇怪。你可以只使用通常的符号和 运行 <= n
,甚至可以跳过 i = 1
直接从 i = 2
:
开始
for (int i = 2; i <= n; i++) {
factorial = factorial * i;
}
您不是在 while 循环中获取输入后计算阶乘,而是在 while 循环之前计算一次阶乘,并在每次给出新输入时打印它。
按如下方式更新主要功能,它应该可以工作。
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int n=1;
while (n >= 1) {
System.out.print("Enter a non-negative integer (-1 to quit) : ");
n = keyboard.nextInt();
// Calculate the factorial for each number you take as the input
int factorial = Factorial(n);
System.out.println(n + "! = " + factorial);}
if (n == 0) {
System.out.print(n = 1); }
if (n == -1) {
System.out.print("Goodbye!"); }
}
public static void main(String[] args) {
// Luke Mihalovich
Scanner keyboard = new Scanner(System.in);
int n;
System.out.print("Enter a non-negative integer (-1 to quit) : ");
n = keyboard.nextInt();
int factorial = Factorial(n);
while (n >= 1) {
System.out.print("Enter a non-negative integer (-1 to quit) : ");
n = keyboard.nextInt();
System.out.println(n + "! = " + factorial);}
if (n == 0) {
System.out.print(n = 1); }
if (n == -1) {
System.out.print("Goodbye!"); }
}
public static int Factorial(int n) {
int factorial = 1;
for(int i= 1;i<n;) {
i++;
factorial = factorial * i; }
return factorial;
}
}
此程序为所有输入打印相同的结果。
例如,如果我输入5
,正确答案是5! = 120
。但是如果我输入4
,它又打印了4! = 120
,这是错误的,应该是24
.
说明
您写道:
int factorial = Factorial(n);
while (n >= 1) {
// ...
System.out.println(n + "! = " + factorial);
}
没有更新 factorial
。像
int factorial = Factorial(n);
仅执行 一次 而不是 live-linked 以便在您更新 n
.
解决方案
因此您需要在每次迭代中显式更新 factorial
:
int factorial = Factorial(n);
while (n >= 1) {
// ...
factorial = Factorial(n);
System.out.println(n + "! = " + factorial);
}
备注
请遵守命名约定。方法名称应始终以 小写字母 字符开头,因此应改为 factorial
。然后变量应该是re-named,例如result
。
你的for-loop看起来很奇怪。你可以只使用通常的符号和 运行 <= n
,甚至可以跳过 i = 1
直接从 i = 2
:
for (int i = 2; i <= n; i++) {
factorial = factorial * i;
}
您不是在 while 循环中获取输入后计算阶乘,而是在 while 循环之前计算一次阶乘,并在每次给出新输入时打印它。
按如下方式更新主要功能,它应该可以工作。
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int n=1;
while (n >= 1) {
System.out.print("Enter a non-negative integer (-1 to quit) : ");
n = keyboard.nextInt();
// Calculate the factorial for each number you take as the input
int factorial = Factorial(n);
System.out.println(n + "! = " + factorial);}
if (n == 0) {
System.out.print(n = 1); }
if (n == -1) {
System.out.print("Goodbye!"); }
}