中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

做網(wǎng)站公司漢獅網(wǎng)絡(luò)開封網(wǎng)站設(shè)計

做網(wǎng)站公司漢獅網(wǎng)絡(luò),開封網(wǎng)站設(shè)計,做網(wǎng)站起什么名字比較好,網(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)變量初始化; wh…

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)條件);
  1. dowhile 是關(guān)鍵字,也有循環(huán)四要素,只是位置不同。
  2. do...while 先執(zhí)行語句,再判斷條件,意味著循環(huán)體至少執(zhí)行一次。
  3. while 后面有一個分號。
  4. whiledo...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ì)介紹,涵蓋了 whiledo...while 循環(huán)的基本語法、執(zhí)行流程及示例。希望對你理解 Java 循環(huán)控制有所幫助!

http://www.risenshineclean.com/news/38825.html

相關(guān)文章:

  • 射洪哪里可以做網(wǎng)站北京seo推廣外包
  • 寧波營銷型網(wǎng)站建設(shè)網(wǎng)絡(luò)銷售平臺有哪些
  • 新風(fēng)格網(wǎng)站灰色詞排名代做
  • 上海工商登記查詢系統(tǒng)南昌網(wǎng)站優(yōu)化公司
  • 順德公益網(wǎng)站制作seo網(wǎng)站推廣
  • 去年做的電子請?zhí)趺凑以W(wǎng)站百度的網(wǎng)頁地址
  • 網(wǎng)站建設(shè)咨詢公企業(yè)網(wǎng)站建設(shè)門戶
  • 網(wǎng)站定位策劃小紅書關(guān)鍵詞優(yōu)化
  • 佛山網(wǎng)站建設(shè)公司哪家性價比高百度競價代運營托管
  • 西安市城鄉(xiāng)建設(shè)檔案館網(wǎng)站域名注冊網(wǎng)站
  • 核名查詢系統(tǒng)seo如何優(yōu)化
  • 網(wǎng)站開發(fā)計劃書模板淘寶引流推廣平臺
  • 如何進(jìn)行優(yōu)化霸屏seo服務(wù)
  • 企業(yè)站用什么程序做網(wǎng)站友情鏈接樣式
  • 大連市營商環(huán)境建設(shè)局網(wǎng)站網(wǎng)絡(luò)銷售平臺上市公司有哪些
  • 網(wǎng)站二級域名怎么設(shè)置小紅書關(guān)鍵詞搜索量查詢
  • 桂林網(wǎng)站建設(shè)內(nèi)容瀏覽器2345網(wǎng)址導(dǎo)航下載安裝
  • 馬云將來淘汰的十個行業(yè)網(wǎng)站建設(shè)網(wǎng)站如何添加友情鏈接
  • 做網(wǎng)站與網(wǎng)店運營如何免費創(chuàng)建自己的網(wǎng)站平臺
  • wordpress活動召集插件seo內(nèi)部優(yōu)化具體做什么
  • 學(xué)做網(wǎng)站書籍微商營銷
  • 軟件app研發(fā)seo優(yōu)化標(biāo)題 關(guān)鍵詞
  • 怎么做網(wǎng)站后臺 更新日志網(wǎng)絡(luò)市場調(diào)研的方法
  • 網(wǎng)站偽靜態(tài)怎么設(shè)置seowhy官網(wǎng)
  • 網(wǎng)站仿做軟件seo是什么意思職業(yè)
  • 建設(shè)工程招標(biāo)網(wǎng)站互聯(lián)網(wǎng)推廣運營是做什么的
  • 廣東出現(xiàn)新病毒是真的嗎關(guān)鍵詞優(yōu)化方法有什么步驟
  • 上海 做網(wǎng)站線下推廣方法有哪些
  • 定制網(wǎng)站開發(fā)哪家強某網(wǎng)站seo診斷分析和優(yōu)化方案
  • iis網(wǎng)站日志今日國際新聞頭條新聞