做網(wǎng)站公司漢獅網(wǎng)絡(luò)開封網(wǎng)站設(shè)計
Java 循環(huán)控制詳解【W(wǎng)hile & do… while】
在 Java 中,循環(huán)控制是程序設(shè)計中非常重要的部分,主要包括 while
循環(huán)和 do...while
循環(huán)。本文將詳細(xì)介紹這兩種循環(huán)的基本語法、執(zhí)行流程及相關(guān)示例。
1. while
循環(huán)控制
基本語法
循環(huán)變量初始化;
while(循環(huán)條件){循環(huán)語句;循環(huán)變量迭代;
} //while 循環(huán)也有四要素 只是四要素放的位置和For不一樣
while
循環(huán)的四要素與 for
循環(huán)類似,只是放置的位置不同。
執(zhí)行流程分析
- 循環(huán)條件是返回一個布爾值的表達(dá)式。
while
循環(huán)是先判斷條件再執(zhí)行語句。
示例 1
題目:編寫一個程序,使用 while 循環(huán)輸出 “Hello this is Yhame.” 和一個遞增的數(shù)字,從 1 到 10。循環(huán)結(jié)束后,輸出當(dāng)前的數(shù)字值。
public class While01 {public static void main(String[] args) {int i = 1;while(i <= 10) {System.out.println("Hello this is Yhame." + i);i++; // 循環(huán)迭代變量}System.out.println("推出循環(huán),繼續(xù)。。。" + i); // 這里 i = 11}
}
示例 2
題目:編寫一個程序,使用 while 循環(huán)輸出 1 到 100 之間所有能被 3 整除的數(shù)字。
public class WhileExercise01 {public static void main(String[] args) {int i = 1;while(i <= 100) {if(i % 3 == 0) {System.out.println("i = " + i);}i++; // 將 i++ 寫在 if 語句內(nèi)部,會導(dǎo)致 i 在其他情況下無法遞增,最終引發(fā)無限循環(huán)。}System.out.println("程序繼續(xù)運行");}
}
示例 3
題目:編寫一個程序,使用 while 循環(huán)輸出 40 到 200 之間的所有偶數(shù)。
public class WhileExercise02 {public static void main(String[] args) {int i = 40;while(i >= 40 && i <= 200) {if(i % 2 == 0) {System.out.println("i = " + i);}i++; // 不能寫在 if 循環(huán)里面; 將 i++ 寫在 if 語句內(nèi)部,會導(dǎo)致 i 在奇數(shù)情況下無法遞增,最終引發(fā)無限循環(huán)。}System.out.println("The process continues");}
}
2. do...while
循環(huán)控制
基本語法
循環(huán)變量初始化;
do {循環(huán)語句;循環(huán)變量迭代;
} while(循環(huán)條件);
do
和while
是關(guān)鍵字,也有循環(huán)四要素,只是位置不同。do...while
先執(zhí)行語句,再判斷條件,意味著循環(huán)體至少執(zhí)行一次。while
后面有一個分號。while
和do...while
的區(qū)別需要注意。
do … while 循環(huán)的執(zhí)行流程
示例代碼
題目:編寫一個程序,使用 do…while 循環(huán)輸出 “Hello This is Yhame!”,直到循環(huán)變量達(dá)到 10 次。循環(huán)結(jié)束后,輸出 “The process continues!”。
public class DoWhile01 {public static void main(String[] args) {int i = 1; // 循環(huán)變量的初始化do {System.out.println("Hello This is Yhame!");i++; // 循環(huán)迭代變量} while(i <= 10);System.out.println("The process continues!");}
}
結(jié)果
do...while
練習(xí) 1
題目:編寫一個程序,使用 do…while 循環(huán)輸出從 1 到 100 的數(shù)字。
public class DoWhileExercise01 {public static void main(String[] args) {int i = 1;do {System.out.println(i);i++;} while(i >= 0 && i <= 100);}
}
do...while
練習(xí) 2
題目:編寫一個程序,使用 do…while 循環(huán)計算并輸出從 1 到 100 的數(shù)字之和。
public class DoWhileExercise02 {public static void main(String[] args) {int i = 1;int sum = 0;do {sum += i;i++;} while(i > 0 && i <= 100);System.out.println("總和為" + sum);System.out.println("Procedure in progress!");}
}
do...while
練習(xí) 3
題目:編寫一個程序,使用 do…while 循環(huán)統(tǒng)計 1 到 200 之間能被 5 整除但不能被 3 整除的數(shù)字的個數(shù),并輸出這些數(shù)字。
public class DoWhileExercise03 {public static void main(String[] args) {// 統(tǒng)計1--200之間能被5整除但不能被3整除的個數(shù)// (1) 使用do...while輸出 1--200// (2) 過濾能被5整除但不能被3整除的數(shù)// (3) 統(tǒng)計滿足條件的個數(shù) int count = 0;int i = 1;int count = 0;do {if(i % 5 == 0 && i % 3 != 0) {// i++; 這里添加i++會讓程序跳不出循環(huán)System.out.println("i = " + i);count++;}i++;} while(i > 0 && i <= 200);System.out.println("這樣的數(shù)一共有:" + count);}
}
do...while
練習(xí) 4
題目:編寫一個程序,模擬一個對話,問用戶是否還錢。如果用戶的回答不是 ‘y’,則繼續(xù)詢問并輸出 “Yhame使出五連鞭!”。直到用戶回答 ‘y’ 為止,最后輸出 “還挺懂事”。
import java.util.Scanner;public class DoWhileExercise4 {public static void main(String[] args) {// 如果李三不還錢,則Yhame一直使出五連鞭,直到李三說還錢為止// [System.out.println("Yhame問:還錢嗎? (y/n)")] do...whileScanner in = new Scanner(System.in);char answer = ' ';do {System.out.println("Yhame問:還錢嗎?(y/n)");answer = in.next().charAt(0); // 獲取輸入的第一個字符System.out.println("他的回答是" + answer);System.out.println("Yhame使出五連鞭!");} while(answer != 'y');System.out.println("還挺懂事");}
}
以上是關(guān)于 Java 循環(huán)控制的詳細(xì)介紹,涵蓋了 while
和 do...while
循環(huán)的基本語法、執(zhí)行流程及示例。希望對你理解 Java 循環(huán)控制有所幫助!