您的位置:首页 > 其它

第7周作业1-循环大战

2014-04-20 12:45 441 查看
FactorialTest



import java.util.Scanner;
public class FactorialTest {
/**
* 計算階乘
*/
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("請問你要計算到哪個數的階乘?請輸入一個整數值:");
int n = input.nextInt();
int t = 1;
//用for循環求階乘
System.out.println("for循環求階乘");
for(int i=1;i<=n;i++){
t=t*i;
System.out.println(i + "!=" + t);
}
System.out.println();

System.out.println("while循環求階乘:");
//用while求階乘
//用循環變量
int m =1;
//t重新設為1
t =1;
while(m<=n){
t = t * m;
System.out.println(m +"!=" + t);
m++;
}
System.out.println();

System.out.println("do...while求階乘:");
//do...while求階乘
int j = 1;
t = 1;
do {
t =t*j;
System.out.println(j + "!=" + t);
j++;
}while(j<=n);
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: