使用JAVA求n的阶乘,用for循环来实现题目
int result = 1;
for (int i = 2; i <=n ; i++){
result *= i;
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
System.out.println(result);
}
}
输出示例
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
n=in.nextInt();
int i=1,sum=1;
for(i=1;i<=n;i++)
{
sum=sum*i;
}
System.out.print(sum);
}
}