当前位置: 首页 > news >正文

企业网站鉴赏seo教学免费课程霸屏

企业网站鉴赏,seo教学免费课程霸屏,网站首页滚动图片怎么做,凡客官方网Java 循环控制详解【While & do… while】 在 Java 中,循环控制是程序设计中非常重要的部分,主要包括 while 循环和 do...while 循环。本文将详细介绍这两种循环的基本语法、执行流程及相关示例。 1. while 循环控制 基本语法 循环变量初始化; wh…

Java 循环控制详解【While & do… while】

在这里插入图片描述

在 Java 中,循环控制是程序设计中非常重要的部分,主要包括 while 循环和 do...while 循环。本文将详细介绍这两种循环的基本语法、执行流程及相关示例。

1. while 循环控制

基本语法

循环变量初始化;
while(循环条件){循环语句;循环变量迭代;
} //while 循环也有四要素 只是四要素放的位置和For不一样

while 循环的四要素与 for 循环类似,只是放置的位置不同。

执行流程分析

在这里插入图片描述

  • 循环条件是返回一个布尔值的表达式。
  • while 循环是先判断条件再执行语句。

示例 1

题目:编写一个程序,使用 while 循环输出 “Hello this is Yhame.” 和一个递增的数字,从 1 到 10。循环结束后,输出当前的数字值。

public class While01 {public static void main(String[] args) {int i = 1;while(i <= 10) {System.out.println("Hello this is Yhame." + i);i++; // 循环迭代变量}System.out.println("推出循环,继续。。。" + i); // 这里 i = 11}
}

示例 2

题目:编写一个程序,使用 while 循环输出 1 到 100 之间所有能被 3 整除的数字。

public class WhileExercise01 {public static void main(String[] args) {int i = 1;while(i <= 100) {if(i % 3 == 0) {System.out.println("i = " + i);}i++; // 将 i++ 写在 if 语句内部,会导致 i 在其他情况下无法递增,最终引发无限循环。}System.out.println("程序继续运行");}
}

示例 3

题目:编写一个程序,使用 while 循环输出 40 到 200 之间的所有偶数。

public class WhileExercise02 {public static void main(String[] args) {int i = 40;while(i >= 40 && i <= 200) {if(i % 2 == 0) {System.out.println("i = " + i);}i++; // 不能写在 if 循环里面; 将 i++ 写在 if 语句内部,会导致 i 在奇数情况下无法递增,最终引发无限循环。}System.out.println("The process continues");}
}

2. do...while 循环控制

基本语法

循环变量初始化;
do {循环语句;循环变量迭代;
} while(循环条件);
  1. dowhile 是关键字,也有循环四要素,只是位置不同。
  2. do...while 先执行语句,再判断条件,意味着循环体至少执行一次。
  3. while 后面有一个分号。
  4. whiledo...while 的区别需要注意。
do … while 循环的执行流程

在这里插入图片描述

示例代码

题目:编写一个程序,使用 do…while 循环输出 “Hello This is Yhame!”,直到循环变量达到 10 次。循环结束后,输出 “The process continues!”。

public class DoWhile01 {public static void main(String[] args) {int i = 1; // 循环变量的初始化do {System.out.println("Hello This is Yhame!");i++; // 循环迭代变量} while(i <= 10);System.out.println("The process continues!");}
}
结果

在这里插入图片描述

do...while 练习 1

题目:编写一个程序,使用 do…while 循环输出从 1 到 100 的数字。

public class DoWhileExercise01 {public static void main(String[] args) {int i = 1;do {System.out.println(i);i++;} while(i >= 0 && i <= 100);}
}

do...while 练习 2

题目:编写一个程序,使用 do…while 循环计算并输出从 1 到 100 的数字之和。

public class DoWhileExercise02 {public static void main(String[] args) {int i = 1;int sum = 0;do {sum += i;i++;} while(i > 0 && i <= 100);System.out.println("总和为" + sum);System.out.println("Procedure in progress!");}
}

do...while 练习 3

题目:编写一个程序,使用 do…while 循环统计 1 到 200 之间能被 5 整除但不能被 3 整除的数字的个数,并输出这些数字。

public class DoWhileExercise03 {public static void main(String[] args) {// 统计1--200之间能被5整除但不能被3整除的个数// (1) 使用do...while输出 1--200// (2) 过滤能被5整除但不能被3整除的数// (3) 统计满足条件的个数 int count = 0;int i = 1;int count = 0;do {if(i % 5 == 0 && i % 3 != 0) {// i++; 这里添加i++会让程序跳不出循环System.out.println("i = " + i);count++;}i++;} while(i > 0 && i <= 200);System.out.println("这样的数一共有:" + count);}
}

do...while 练习 4

题目:编写一个程序,模拟一个对话,问用户是否还钱。如果用户的回答不是 ‘y’,则继续询问并输出 “Yhame使出五连鞭!”。直到用户回答 ‘y’ 为止,最后输出 “还挺懂事”。

import java.util.Scanner;public class DoWhileExercise4 {public static void main(String[] args) {// 如果李三不还钱,则Yhame一直使出五连鞭,直到李三说还钱为止// [System.out.println("Yhame问:还钱吗? (y/n)")] do...whileScanner in = new Scanner(System.in);char answer = ' ';do {System.out.println("Yhame问:还钱吗?(y/n)");answer = in.next().charAt(0); // 获取输入的第一个字符System.out.println("他的回答是" + answer);System.out.println("Yhame使出五连鞭!");} while(answer != 'y');System.out.println("还挺懂事");}
}

以上是关于 Java 循环控制的详细介绍,涵盖了 whiledo...while 循环的基本语法、执行流程及示例。希望对你理解 Java 循环控制有所帮助!

http://www.tj-hxxt.cn/news/63842.html

相关文章:

  • 网站建设哪家公司好 电商 b2c公司seo营销
  • 九江网站制作网店代运营一年的费用是多少
  • 网站建设又叫什么好看的web网页
  • 外贸婚纱礼服网站优化算法
  • 没有网站可以做cpa吗seo行业岗位
  • 百度推广seo怎么学重庆搜索引擎seo
  • 上海广告投放公司国内seo服务商
  • 繁体网站模板软文网官网
  • 湖南长沙旅游攻略自助游西安seo排名收费
  • 人工智能设计系统公司优化公司组织架构
  • 百度网站提交入口网址无线网络优化
  • 如何撰写网站建设方案书免费数据分析网站
  • 关于我们做网站护肤品软文推广
  • 老榕树智能建站系统关键词怎么选择技巧
  • 网站建设公司哪个好呀net网站建设品牌推广方案案例
  • 你会怎么做外国的网站吗seo教程 百度网盘
  • 可以做bim实操题的网站搜索推广渠道
  • 洛阳做网站推广浏览器下载安装
  • 网站备案登记查询系统武汉网站seo服务
  • 网站链接云数据库百度推广电话号码
  • dede 手机站 怎么获取跳转网站免费刷赞网站推广免费
  • 自己如何在网上做网站百度一下 你就知道官网 新闻
  • 做网站费用多少seo站长之家
  • 上线了网站谷歌浏览器手机版免费官方下载
  • 网页制作与设计考的在哪查房百度词条优化
  • 武汉做网站找互赢网络品牌推广和品牌营销
  • 公司外贸网站怎么做网站推广常用方法
  • 洛阳 网站建设公司微信指数查询
  • 济南做外贸网站网站备案查询工信部
  • 软件外包价格思亿欧seo靠谱吗