Java中dowhile循环
一、语法
初始化变量
do{
循环体;
迭代器自增;
}while(循环条件);
注意:
1.特点是先做后判断,即使不满足条件,也会做一次。
2.while();后面的分号不能省略
二、举例
int circulate;
do{
System.out.println(“循环体”);
circulate++;
}while(circulate<10);
三、嵌套
初始化变量
do{
初始化变量
do{
循环体;
}while(循环条件);
迭代器自增;
}while(循环条件);
四、例子
public class Test01 {
public static void main(String[] args) {
// 1000-2023闰年
int year = 1000;
int sum = 0;
int count=0;
do {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
System.out.print(year + " ");
sum++;
count++;
}
//五个一组
if(count==5){
System.out.println();
count=0;
}
year++;
} while (year <= 2023);
//计数换行
System.out.println();
System.out.println("共有:" + sum + "个");
}
}
还没有评论,来说两句吧...