Java 實(shí)例 - continue 關(guān)鍵字用法
Java continue 語(yǔ)句語(yǔ)句用來(lái)結(jié)束當(dāng)前循環(huán),并進(jìn)入下一次循環(huán),即僅僅這一次循環(huán)結(jié)束了,不是所有循環(huán)結(jié)束了,后邊的循環(huán)依舊進(jìn)行。
以下實(shí)例使用了 continue 關(guān)鍵字來(lái)跳過當(dāng)前循環(huán)并開始下一次循環(huán):
/* author by w3cschool.cn Main.java */ public class Main { public static void main(String[] args) { StringBuffer searchstr = new StringBuffer( "hello how are you. "); int length = searchstr.length(); int count = 0; for (int i = 0; i < length; i++) { if (searchstr.charAt(i) != 'h') continue; count++; searchstr.setCharAt(i, 'h'); } System.out.println("發(fā)現(xiàn) " + count + " 個(gè) h 字符"); System.out.println(searchstr); } }
以上代碼運(yùn)行輸出結(jié)果為:
發(fā)現(xiàn) 2 個(gè) h 字符 hello how are you.
更多建議: