第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); } }
相关文章推荐
- 第7周作业1-循环大战do...while
- 第7周作业1-循环大战FactorialTest
- 第7周作业1-循环大战
- 第7周作业1-循环大战for
- 第7周作业1-循环大战
- 第7周作业1-循环大战------while
- 第7周作业1-循环大战
- 第7周作业1-循环大战
- 第7周作业1-循环大战for
- 第7周作业1-循环大战(DO-WHILE语句)
- 第7周作业1-循环大战(FOR语句)
- 第7周作业1-循环大战
- 第7周作业1-循环大战(WHILE语句)
- 第7周作业1-循环大战
- 第7周作业1-循环大战
- 第7周作业1-循环大战while
- 第7周作业1-循环大战for
- 第7周作业1-循环大战
- 第7周作业1-循环大战。
- 第7周作业1-循环大战while先行版