




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第5章累加与阶乘 循环结构 能力目标 学会使用for while和do while循环语句 理解递归调用方法 学会使用加赋值 乘赋值等复合赋值运算符 能运用循环结构编写计算累加 阶乘以及乘法表等应用程序 内容介绍 5 1任务预览5 2while语句5 3复合赋值运算符5 4for语句5 5递归调用方法5 6do while语句5 7break和continue语句5 8多重循环5 9本章小结5 10实训5 累加 阶乘与乘法表 5 1任务预览 本章实训程序运行结果 5 2while语句 循环语句有3个 while for和do while while循环语句的语法形式如下 while 条件表达式 循环体 例5 1 编程 计算1到10的累加 即1 2 3 10 intsum 0 inti 1 while i 10 sum sum i System out printf 1到 d的累加结果 d n i sum i System out printf 最后结果 d sum 算法描述 1 0 sum 2 1 i 3 当i 10时 执行下一步 否则跳转到步骤 8 4 sum i sum 5 输出中间结果sum 6 i 1 i 7 转回步骤 3 8 输出最后结果sum 5 3复合赋值运算符 复合赋值运算符有 执行上述语句 将输出 Wearestudents 5 4for语句 for 变量初始化 条件表达式 循环变量更新 循环体 例5 2 编程 使用for语句计算1到10的累加 publicclassExample2 publicstaticvoidmain String args intsum 0 for inti 1 i 10 i sum i 加赋值运算System out printf 1到 d的累加结果 d n i sum System out printf 最后结果 d sum 例5 3 阶乘程序1 使用for循环语句计算10的阶乘 publicclassExample3 publicstaticvoidmain String args intfactorial 1 for inti 1 i 10 i factorial i 乘赋值运算System out printf d的阶乘 d n i factorial System out printf 最后结果 d factorial 例5 4 阶乘程序2 使用方法调用计算10的阶乘 publicclassExample4 staticlongcalcFactorial intn 计算n的阶乘方法longfactorial 1 for inti n i 1 i factorial i returnfactorial publicstaticvoidmain String args System out printf 最后结果 d calcFactorial 10 调用阶乘方法 5 5递归调用方法 数学上用n 表示n的阶乘 数学公式如下 n n n 1 若n 1 n 1 若n 1 例5 5 阶乘程序3 使用递归调用方法计算10的阶乘 publicclassExample5 staticlongcalcFactorial intn 递归方法if n 1 returnn calcFactorial n 1 递归调用else return1 publicstaticvoidmain String args System out printf 最后结果 d calcFactorial 10 例5 6 使用递归调用编写计算f n 的方法 并调用方法计算f 6 f 7 f 8 的值 其中 f n f n 1 f n 2 若n 3 f n 1 若n 3 publicclassExample6 staticintf intn 递归调用方法if n 3 returnf n 1 f n 2 else return1 publicstaticvoidmain String args System out printf f d d n 6 f 6 System out printf f d d n 7 f 7 System out printf f d d n 8 f 8 5 6do while语句 do循环体while 条件表达式 例5 7 编程 使用do循环语句计算1到10的累加 publicclassExample7 publicstaticvoidmain String args intsum 0 inti 1 do sum i System out printf 1到 d的累加结果 d n i sum i while i 10 System out printf 最后结果 d sum 5 7break和continue语句 break可跳出多分支语句switch和循环语句 continue只能用于循环语句 结束本轮 继续下轮循环 例5 8 编程 求1到10的累加 要求使用break和continue语句 intsum 0 inti 1 while true sum i System out printf 1到 d的累加结果 d n i sum i if i 10 continue else break System out printf 最后结果 d sum 例5 9 编程 计算1到20中除5 15以外的所有奇数的平方 但若平方值超过300 则终止 publicclassExample9 publicstaticvoidmain String args intsquare for inti 1 i300 break System out printf d的平方 d n i square 5 8多重循环 二重以上的循环就是多重循环 例5 10 编程 使用二重循环 输出8行4列的表格 publicclassExample10 publicstaticvoidmain String args for inti 1 i 8 i i控制行for intj 1 j 4 j j控制列System out printf d行 d列 i j System out println 换行 例5 11 编程 使用二重循环 计算并输出乘法表 publicclassExample11 publicstaticvoidmain String args for inti 1 i 9 i i控制行for intj 1 j 9 j j控制列System out printf d d 2d j i j i System out println 换行 5 9本章小结 编写循环结构主要使用循环语句 Java有3个循环语句 while for和do while 其中for语句使用频率最多 一些递归形式的数学函数或公式 除了使用循环语句 也可以运用递归调用方法进行编程 实质上递归调用隐含了循环结构 复合赋值运算符如加赋值 乘赋值等 这些运算符把两种运算合并成一种 显得非常简练 可在循环语句中使用加赋值进行累加运算 使用break语句终止循环语句 要中止本次循环而继续下一轮循环 则使用continue语句 循环语句也可相互嵌套 形成多重循环 如二重循环 5 10实训5 累加 阶乘与乘法表 1 编写计算1到n的累加程序 要求程序运行时输入正整数
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 南大校区围堰工程施工方案
- 民宿管理面试题库及答案
- 2025年教师招聘之《小学教师招聘》题库必刷100题含答案详解【典型题】
- 教师招聘之《小学教师招聘》综合提升试卷及答案详解【考点梳理】
- 2025年教师招聘之《幼儿教师招聘》每日一练试卷附参考答案详解(夺分金卷)
- 2025年教师招聘之《幼儿教师招聘》每日一练试卷附参考答案详解(能力提升)
- 教师招聘之《小学教师招聘》综合提升练习试题含答案详解【黄金题型】
- 2025年艾梅乙培训试题(含答案)
- 共青餐饮联合整改措施
- 教师招聘之《幼儿教师招聘》考前冲刺练习试题含答案详解(巩固)
- 2024年秋季新统编版七年级上册道德与法治全册教案
- GB/T 37977.46-2024静电学第4-6 部分:特定应用中的标准试验方法腕带
- 《矿物岩石学》全套教学课件
- 不动产授权委托书样本
- 全国职业院校宠物营养学知识竞赛备考试题库(含答案)
- 休产假工作交接表
- 心理健康五年级上册北师大版第二课 交往从尊重开始 课件
- 护士自我管理规划
- 航海英语会话(一)
- 医疗器械关于生产和生产后信息评价(模板)
- 2023年度出版专业职业资格考试试题及参考答案初级
评论
0/150
提交评论