do…while循環(huán)語句格式
基本格式
do {
循環(huán)體語句;
}while((判斷條件語句);
擴(kuò)展格式
初始化語句;
do {
循環(huán)體語句;
控制條件語句;
} while((判斷條件語句);
執(zhí)行流程圖
do...while執(zhí)行流程圖
package com.itheima_06;
/*
* do...循環(huán)的語句格式:
* do {
* 循環(huán)體語句;
* }while(判斷條件語句);
*
* 擴(kuò)展格式:
* 初始化語句;
* do {
* 循環(huán)體語句;
* 控制條件語句
* }while(判斷條件語句);
*
* 執(zhí)行流程:
* A:執(zhí)行初始化語句
* B:執(zhí)行循環(huán)體語句
* C:執(zhí)行控制條件語句
* D:執(zhí)行判斷條件語句宙拉,看是true還是false
* 如果是false腺怯,就結(jié)束循環(huán)
* 如果是true舵盈,就回到B繼續(xù)
*
* 練習(xí):
* 1.用do...while實(shí)現(xiàn)求和案例
* 2.統(tǒng)計水仙花個數(shù)案例
*/
public class DoWhileDemo {
public static void main(String[] args) {
//在控制臺輸出10次HelloWorld
/*
for(int x = 1; x <= 10; x++) {
System.out.println("HelloWorld");
}
*/
//do..while改寫
int x = 1;
do {
System.out.println("HelloWorld");
x++;
}while(x <= 10);
//求和案例
//定義變量
int a = 1;
int sum = 0;
do {
a++;
sum += a;
}while(a <= 10);
System.out.println("sum=" + sum);
//統(tǒng)計水仙花個數(shù)案例
//定義變量
int xx = 100;
int count = 0;
do {
xx++;
int ge = xx % 10;
int shi = xx / 10 % 10;
int bai = xx / 10 / 10 % 10;
if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == xx) {
count++;
}
}while(xx <= 999);
System.out.println("水仙花的個數(shù)為:" + count);
}
}