整體結構
do-while 迴圈在檢查條件之前至少執行一次語句。
一般形式為:
do { instruções; } while(condição);
for 和 while 迴圈之間的差異
簡單範例
循環直到使用者輸入字母「q」的程式:
// Demonstra o laço do-while. class DWDemo { public static void main(String args[]) throws java.io.IOException { char ch; do { System.out.print("Press a key followed by ENTER: "); ch = (char) System.in.read(); // obtém um char } while(ch != 'q'); } }
改良的猜謎遊戲
一個循環的猜測程序,直到使用者猜出正確的字母:
參見Guess4.java
占卜程序解說
執行範例
猜測程序的典型執行
I'm thinking of a letter between A and Z. Can you guess it: A ...Sorry, you're too low Try again! I'm thinking of a letter between A and Z. Can you guess it: Z ...Sorry, you're too high Try again! I'm thinking of a letter between A and Z. Can you guess it: K ** Right **
重要細節
結論
以上是Java 中的 do-while 循環的詳細內容。更多資訊請關注PHP中文網其他相關文章!