Java中dowhile循环

不念不忘少年蓝@ 2024-03-26 15:39 191阅读 0赞

一、语法

初始化变量

do{

循环体;

迭代器自增;

}while(循环条件);

注意:

1.特点是先做后判断,即使不满足条件,也会做一次。

2.while();后面的分号不能省略

二、举例

int circulate;

do{

System.out.println(“循环体”);

circulate++;

}while(circulate<10);

三、嵌套

初始化变量

do{

初始化变量

do{

循环体;

}while(循环条件);

迭代器自增;

}while(循环条件);

四、例子

  1. public class Test01 {
  2. public static void main(String[] args) {
  3. // 1000-2023闰年
  4. int year = 1000;
  5. int sum = 0;
  6. int count=0;
  7. do {
  8. if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
  9. System.out.print(year + " ");
  10. sum++;
  11. count++;
  12. }
  13. //五个一组
  14. if(count==5){
  15. System.out.println();
  16. count=0;
  17. }
  18. year++;
  19. } while (year <= 2023);
  20. //计数换行
  21. System.out.println();
  22. System.out.println("共有:" + sum + "个");
  23. }
  24. }

发表评论

表情:
评论列表 (有 0 条评论,191人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java循环断、循环嵌套

    循环中断 1.break 直接强行跳出当前循环,不再执行剩余代码。(作用于整个循环) ps:在多重循环情况下,如果break在内层循环中,则仅仅终止了内层循环,外

    相关 Javafor循环

    一、语法 1.标准格式 for(初始化变量;循环条件;迭代器自增)\{ 循环体; \} 2.变化格式 1) 初始化变量; for(;循环条件;迭