java中的4種循環(huán)方法示例詳情
java循環(huán)結構
順序結構的程序語句只能 被執(zhí)行一次。如果你要同樣的操作執(zhí)行多次,就需要使用循環(huán)結構。
java中有三種主要的循環(huán)結構: while 循環(huán) do...while 循環(huán) for 循環(huán) 在java5中引入一種主要用于數(shù)組的增強型for循環(huán)。
1.while循環(huán)
while是最基本的循環(huán),它的結構為:
package com.example.lesson1; //while(布爾(true/false)表達式){ //循環(huán)內容 //只要布爾表達式為 true 循環(huán)體就會一直執(zhí)行下去。 //來看看實例吧: public class Test { public static void main(String args[]) { int x = 10; while (x < 20) { System.out.print("value of x :" + x); x++; System.out.print("\n"); } } }
以上實例編譯運行結構如下:
value of x : 10 value of x : 11 ... value of x : 19
2.do…while循環(huán)
對于while語句而言,如果不滿足條件,則不能進入循環(huán)。但有時候我們需要即使不滿足條件,也至少 執(zhí)行一次。
do…while循環(huán)和while循環(huán)相同,不同的是,
do…while循環(huán)至少會執(zhí)行一次。
package com.example.lesson1; //do{ // //代碼語句 // }while(布爾值表達式); // 注意:布爾表達式在循環(huán)體的后面,所以語句塊在檢測布爾表達式之前已經執(zhí)行了。如果布爾表達式值為true,則語句塊 //一直執(zhí)行,直到布爾表達式的值為false。 // 實例: public class Test { public static void main(Staing args[]) { int x = 10; do { System.out.print("value of x :" + x); x++; System.out.print("\n"); } while (x < 20); } }
以上實例編譯運行結果如下:
value of x : 10 ... value of x :19
3.for循環(huán)
雖然所有循環(huán)結構都可以用while或者do…while表示,但java提供了另一種語句(for循環(huán)),使一些循環(huán)結構變得更簡單。
for循環(huán)執(zhí)行的次數(shù)是在執(zhí)行前就確定的。語法格式如下: //for ( 1初始化; 2布爾表達式; 4更新){ 3//代碼語句 //} //關于for循環(huán)有以下幾點說明: //1,最先執(zhí)行初始化步驟??梢月暶饕环N類型,但可初始化一個或多個循環(huán)控制變量,也可以是空語句。 //2,然后,檢測布爾表達式的值。如果是true,循環(huán)體被執(zhí)行,如果是false,循環(huán)體終止,開始執(zhí)行循環(huán)后面的語句。 //3,執(zhí)行一次循環(huán)后,更新循環(huán)控制變量。 //4,再次檢測布爾表達式。循環(huán)執(zhí)行上面的過程。 public class Test{ public static void main (Staing args[ ]){ for(int x=10;x<20;x=x+1){ System.out.print("value of x :"+x); System.out.print("\n"); } } }
編譯運行結果如下
value of x :10 ... value of x :19
4.java 增強for循環(huán)
java5引入一種主要用于數(shù)組的增強型rot循環(huán)。
java增強for循環(huán)語法格式如下:
for(聲明語句:表達式){ //代碼句子 } //聲明語句:聲明新的局部變量,該變量的類型必須和數(shù)組元素的類型匹配。其作用域限定在循環(huán)語句塊 //其值與此時數(shù)組元素的值相等。 //表達式:表達式是要訪問的數(shù)組名,或者是返回值為數(shù)組的方法。 //實例: public class test { public static void main(String args[]) { int[] numbers = { 10, 20, 30, 40, 50 }; for (int x : numbers) { System.out.print(x); System.out.print(","); } System.out.print("\n"); String[] names = { "James", "Larry", "Tom", "Lacy" }; for (String name : names) { System.out.print(name); System.out.print(","); } } }
編譯運行如下:
10,20,30,40,50, James,Larry,Tom,Lacy,
break關鍵字
break主要用在循環(huán)語句或者switch語句中,用來跳出整個語句塊。 break跳出最里面的循環(huán),并且繼續(xù)執(zhí)行該循環(huán)下面的語句。
break實例:
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { // x 等于 30 時跳出循環(huán) if( x == 30 ) { break; } System.out.print( x ); System.out.print("\n"); } } }
以上實例編譯運行結果如下:
10 20
continue 關鍵字
continue 適用于任何循環(huán)控制結構中。作用是讓程序立刻跳到下一次循環(huán)的迭代。 在for循環(huán)中,continue語句使程序立即跳轉到更新語句。 在while或者do...while循環(huán)中,程序立即跳轉到布爾表達式的判斷語句。
continue 實例:
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { continue; } System.out.print( x ); System.out.print("\n"); } } }
以上實例編譯運行結果如下:
10 20 40 50
到此這篇關于java中的4種循環(huán)方法示例詳情的文章就介紹到這了,更多相關java循環(huán)方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java實現(xiàn)音樂播放器完整代碼(調整顯示音量大小、調整進度、圖片切換)
這篇文章主要給大家介紹了關于java實現(xiàn)音樂播放器(調整顯示音量大小、調整進度、圖片切換)的相關資料,這本身是老師布置的一個作業(yè),寫完感覺不錯分享給大家,需要的朋友可以參考下2023-07-07Spring實戰(zhàn)之使用p:命名空間簡化配置操作示例
這篇文章主要介紹了Spring實戰(zhàn)之使用p:命名空間簡化配置操作,結合實例形式分析了spring p:命名空間簡單配置與使用操作技巧,需要的朋友可以參考下2019-12-12java連接hdfs ha和調用mapreduce jar示例
這篇文章主要介紹了Java API連接HDFS HA和調用MapReduce jar包,需要的朋友可以參考下2014-03-03