




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
循环结构就是为了解决重复代码或者是有规律的代码 1 为什么需要循环结构 凤姐要谢添说 100 句我爱你 String say 他爱罗玉凤 int count 1 while count 100 System out println 谢添第 count 次说 say count 2 什么是 while 循环结构 基本语法 while 循环结构 while 条件 代码块 循环三要素 1 循环条件 2 执行的代码块 3 改变循环变量 永远不会退出的循环叫做死循环 永远不会退出的循环叫做死循环 4 循环案例 4 1 谢添的女朋友让他说 100 句我爱你 String say 他爱罗玉凤 int count 1 while count 100 System out println 谢添第 count 次说 say count 4 2 打印 1 100 之间所有的数 int count 1 while count 100 System out println count 4 3 打印 1 100 之间所有的偶数 int count 1 while count 100 if count 2 0 System out println count count 4 4 打印 100 200 之间所有能被 3 或者 7 整除的数 int num 100 while num 200 if num 3 0 num 7 0 System out println num num 4 5 计算 1 2 3 100 的和 int count 1 int sum 0 while count 100 sum count sum sum count 将变量值与表达式值相加 并将和赋给该变量 count System out println 1 2 3 100 的和 sum 4 6 计算 1 3 5 99 的和 int count 1 int sum 0 while count 99 sum count count 2 System out println 1 3 5 99 的和 sum 4 7 输入一个数字 计算从 1 到这个数字之间所有数字的和 Scanner input new Scanner System in System out println 请输入一个数字 int num input nextInt int count 1 int sum 0 while count num sum count count System out println 从 1 到 num 之间所有数字的和 sum 4 8 求出从 1 加到 100 时 总和刚好超过 3000 时 那个数的值是多 少 int count 1 int sum 0 while count 3000 System out println count 中断循环 break count 4 9 统计 200 300 之间能被 7 整除的数的个数 int count 200 int total 0 while count 300 if count 7 0 total count System out println 200 300 之间能被 7 整除的数有 total 个 4 10 打印所有的水仙花数 153 1 1 1 5 5 5 3 3 3 int count 100 while count 999 int ge count 10 int shi count 10 10 int bai count 100 if count ge ge ge shi shi shi bai bai bai System out println count count 4 11 打印出 10000 99999 所有的回文数 12321 4 12 有一对兔子 从出生后第 3 个月起每个月都生一对兔子 小兔 子长到第三个月后每个月又生一对兔子 假如兔子都不死 问每个 月的兔子总数为多少 4 13 猴子第一天摘下若干个桃子 当即吃了一半 还不瘾 又多吃 了一个第二天早上又将剩下的桃子吃掉一半 又多吃了一个 以后 每天早上都吃了前一天剩下的一半零一个 到第 10 天早上想再吃时 见只剩下一个桃子了 求第一天共摘了多少 int day 1 int sum 1 while day 10 sum sum 1 2 System out println 第 10 day 天有 sum 桃子 day 4 14 从控制台接收数字并累加 当输入的数字是 0 的时候退出循环 并且输出所有数字累加的和 4 15 从控制台接收字符串并累加 当输入的字符串是 esc 的时候结 束 并输出字符串 4 16 猜数字游戏 import java util Random import java util Scanner public class Guess public static void main String args Random random new Random 随机生成 100 以内的数字 int num random nextInt 100 Scanner input new Scanner System in int count 1 while count 6 System out println 请输入你猜的数字 int op input nextInt if op num System out println 恭喜你 猜对了 奖励罗玉 凤一个 break else if op num System out println 猜小了 else System out println 猜大了 count System out println 笨蛋 猜了那么多
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年砌体工程考试题及答案
- 2025-2026学年地质版(2024)小学体育与健康二年级全一册《别让眼睛受伤害》教学设计
- 2025标准版车位租赁合同
- 葡萄酒试酒知识培训
- 叉车证考试判断题技巧及答案
- 2025短期用工劳动合同协议书
- 现象类题目的模板及答案
- 2025餐饮联盟合作协议合同范本
- 餐饮服务员初级考试题及答案
- 2025按摩师劳动合同范本参考
- GB/T 29245-2012信息安全技术政府部门信息安全管理基本要求
- 中药调剂技术-课件
- 证券从业考试基础模拟卷二(题目+解析)
- 水轮发电机讲义课件
- 姜黄素合成路线
- 信息系统运维服务方案
- 化工试生产总结报告
- 导数与原函数的对称性 微专题课件-2023届高三数学一轮复习
- 安全教育:不私自离开幼儿园
- 刑法各论(第四版全书电子教案完整版ppt整套教学课件最全教学教程)
- 健康教育学【完整版】
评论
0/150
提交评论