JAVA
使用for、while、do…while分别实现n!,并在n=10的情况下测试
建议编写三个独立的方法
代码:
1、for循环:
import java.util.Sxabber;
public class MainArgs {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("输入整数n");
int n=sc.nextInt();
int t=1;
for(int i = 1;i<=n;i++){
t=t*i;
}
System.out.println("t="+t);
}
}
2、While循环:
import java.util.Scanner;
public class MainArgs {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("输入整数n");
int n=sc.nextInt();
int t=1;
int i=1;
while(i<=n){
t=t*i;
i++;
}
System.out.println("n!="+t);
}
}
3、do…while:
import java.util.Scanner;
public class MainArgs {
public static void main(String[] args){
Scanner sc = new
Scanner(System.in);
System.out.println("输入整数n");
int n=sc.nextInt();
int t=1;
int i=1;
do{
t=t*i;
i++; }while(i<=n);
System.out.println("n!="+t);
}
}