/* 循環變量n的值必須先進行初始化; 循環變量n的值必須被改變 */ while(循環條件){ //爲避免死循環,小括號後面不要寫分號 語句; } //若循環體只有一條語句,能夠省略大括號
案例一:java
/** * 使用while循環,求1-5的累加和 */ public class PlusDemo { public static void main(String[] args) { int n = 1; int sum = 0;//存放和的變量 while (n <= 5) { sum += n; n++; } System.out.println("循環結束後n的值爲:" + n); System.out.println("累加和爲:" + sum); } }
案例二:dom
/** * 使用while循環輸出26個小寫英文字母,分兩行輸出 */ public class CharDemo { public static void main(String[] args) { char ch = 'a'; int count = 1;//控制換行 while (ch <= 'z') { System.out.print(ch + " "); if (count % 13 == 0) { System.out.println(); } count++; ch++; } } }
/* do-while循環至少執行一次 循環條件後的分號不能丟 */ do{ //若只有一條語句,大括號能夠省略 語句; }while(循環條件);
Tips:局部變量使用前必須初始化且只在定義它的大括號內有效oop
import java.util.Scanner; /** * 使用do-while循環完成一個猜字遊戲 * 猜一個介於1-10之間的數字,將輸入的數字和實際數字相比較,並給出提示 */ public class GuessDemo { public static void main(String[] args) { /* 利用Math.random()生成隨機數(Math.random()生成的 隨機數範圍是:[0, 1),返回值類型爲double類型) */ int answer = (int) (Math.random() * 10 + 1);//此時是[1, 11)的整型 int guess;//用戶猜想的數字 Scanner sc = new Scanner(System.in); do { System.out.println("請輸入猜想的數字:"); guess = sc.nextInt(); //提示部分 if (guess < answer) { System.out.println("過小"); } else if (guess > answer) { System.out.println("太大"); } } while (guess != answer); System.out.println("正確"); }
/* 表達式1:循環變量初始化,只執行一次 表達式2:循環條件 表達式3:改變循環變量的值 執行順序:1. 表達式1;2. 表達式2;3.語句;4.表達式3 若省略表達式2,for循環將變成死循環 */ for(表達式1; 表達式2; 表達式3){//若只有一條語句能夠省略大括號 語句; }
Tips:變量不能重複定義spa
/** * 使用for循環編寫一個程序 * 求出200到300之間的數,且知足條件:它們三個數字之積爲42,三個數字之和爲12。 */ public class ForLoop { public static void main(String[] args) { int unit;//個位 int decade;//十位 int hundreds;//百位 //使用for循環 for(int n = 200; n <= 300; n++){ //取出百位數 hundreds = n / 100; //取出十位數 decade = n % 100 / 10; //取出個位數 unit = n % 10; //計算三個數字之積 int mul = unit * decade * hundreds; //計算三個數字之和 int sum = unit + decade + hundreds; //若是積等於42而且和爲12,則將知足條件的數輸出 if(mul == 42){ if(sum == 12){ System.out.println(n); } } } } }
while(循環條件){//外重循環 …… while(循環條件){//內重循環 …… } …… }
for(表達式1; 表達式2; 表達式3){//外重循環 …… for(表達式1; 表達式2; 表達式3){//內重循環 …… } …… }
案例一:code
/** * 使用while循環嵌套輸出10行10列的星號 */ public class StarDemo { public static void main(String[] args) { int m = 1;//外重循環的控制變量 int n = 1;//內重循環的控制變量 System.out.println("輸出10行10列的星號"); //外重循環控制輸出幾行 while (m <= 10) { n = 1;//從新給n賦值 //內重循環控制每行輸出幾個星號 while (n <= 10) { System.out.print("* "); n++; } System.out.println(); m++; } } }
案例二:使用for循環嵌套,用星號輸出如圖的梯形:遊戲
public class StarDemo2 { public static void main(String[] args) { for(int i = 1; i <= 5; i++){//外重循環控制行數 for(int j = 1; j <= (5 - i); j++){//第一個內重循環控制輸出空格的數量 System.out.print(" "); } for(int k = 1; k <= (2 * i + 1); k++){//第二個內重循環控制輸出星號的數量 System.out.print("*"); } System.out.println(); } } }
之後會補一篇《Java使用循環打印圖形》
案例三:ip
/** * 利用for循環嵌套輸出1到10的階乘和 */ public class FactorialDemo { public static void main(String[] args) { int mul = 1;//存放階乘的和 int sum = 0;//存放階乘 for(int i = 1; i <= 10; i++){ mul = 1;//重置mul的值 for(int j = 1; j <= i; j++){ mul *= j; } sum += mul; } System.out.println("階乘值爲:"+sum); } }