为何使用此声明:fact*=i;
。我无法理解,因为我是初学者。还有其他方法可以在while循环中编写相同的语句吗?
//This is my program for factorial using input from user.
import java.io.*;
public class factorialInput {
public static void main(String[] args) throws IOException {
int i = 1;
int fact = 1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any number:");
int n = Integer.parseInt(br.readLine());
while (i <= n) {
fact *= i;
i++;
}
System.out.println("Factorial of" + n + "!" + "=" + fact);
}
}
答案 0 :(得分:2)