总体结构
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中文网其他相关文章!