版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第三章程序流程控制回顾Java中的三种注释创建包与导包变量的数据类型作用域局部变量的定义与使用运算符字符串的定义与连接本章目标顺序流程分支流程循环流程本章结构分支流程流程控制循环流程顺序流程for循环do…while循环while循环If语句switch&caseIf…elseIf…elseif…else分支流程分支流程importmon.SystemIn;publicclassTestDiv{publicstaticvoidmain(Stringargs[]){ inta=10; intb=SystemIn.nextInt(); System.out.println("a/b="+a/b);}}if语句if语句if(布尔表达式){
语句内容
语句内容}importmon.SystemIn;publicclassTestDiv{publicstaticvoidmain(Stringargs[]){ inta=10; intb=SystemIn.nextInt(); if(b!=0){ System.out.println("a/b="+a/b); }}}if语句if语句特例if(布尔表达式)语句内容
importmon.SystemIn;publicclassTestDiv{publicstaticvoidmain(Stringargs[]){ inta=10; intb=SystemIn.nextInt(); if(b!=0)System.out.println("a/b="+a/b);}}if…else语句if…else语句if(布尔表达式){
语句内容1}else{
语句内容2}代码演示在下一页if…else语句importmon.SystemIn;publicclassTestDiv{publicstaticvoidmain(Stringargs[]){inta=10;intb=SystemIn.nextInt();if(b!=0){ System.out.println("a/b="+a/b);}else{ System.out.println("对不起除法不可以用0做除数");}}}if…elseif…else语句If…elseif…else语句if(布尔表达式){
语句内容}elseif(布尔表达式){
语句内容}elseif(布尔表达式){
语句内容}…else{
语句内容}if…elseif…else语句importmon.SystemIn;publicclassTestIfElseIf{publicstaticvoidmain(Stringargs[]){//输入分数intscore=SystemIn.nextInt();if(score==5){ System.out.println("你的成绩为优秀");}elseif(score==4){ System.out.println("你的成绩为良好");}elseif(score==3){ System.out.println("你的成绩为及格");}else{ System.out.println("你的成绩为不及格");}}}switch&case语句switch&case语句(尽量不要用)switch(数据){case值1:语句1;case值2
:语句2;….default:语句3;}switch&case语句importmon.SystemIn;publicclassTestSwitchCase{publicstaticvoidmain(Stringargs[]){//输入分数intscore=SystemIn.nextInt();switch(score){case5:System.out.println("你的分数为优秀");case4:System.out.println("你的分数为良好");case3:System.out.println("你的分数为及格");default:System.out.println("你的分数为不及格");}}}switch&case语句importmon.SystemIn;publicclassTestSwitchCase{publicstaticvoidmain(Stringargs[]){//输入分数intscore=SystemIn.nextInt();switch(score){case5:System.out.println("你的分数为优秀");break;case4:System.out.println("你的分数为良好");break;case3:System.out.println("你的分数为及格");break;default:System.out.println("你的分数为不及格");break;}}}switch&case注意事项每一个case声明之后应该有breakdefault语句可以放置在
最后,中间,或
开始处如switch(){default:..case1:System.out.println();break;}switch(){case1:System.out.println();break;default:..}switch&case注意事项-1
确保switch的变量类型是byte,short,char或int之一中Java5中,switch的变量类型还支持枚举类型在JDK7中,switch的变量类型还支持String类型如inta=10switch(a){case1:}Stringname=“suns”;switch(name){case“tom”:}charc=‘A’;switch(c){case‘A’:}switch&case注意事项-2publicclassTestSwithCaseString{publicstaticvoidmain(Stringargs[]){ Stringtemp="ok"; switch(temp){ default:System.out.println("tempvalueisother"); case"ok":System.out.println("tempvalueisok"); case"error":System.out.println("tempvalueiserror"); }}}switch&case注意事项复杂的switch&caseimportmon.SystemIn;publicclassTestSwitchCase{publicstaticvoidmain(Stringargs[]){ //输入分数
intscore=SystemIn.nextInt(); switch(score){ case5:System.out.println("你的分数为优秀");break; case4:{ System.out.println("你的分数为良好"); break; } case3:System.out.println("你的分数为及格");break; default:System.out.println("你的分数为不及格");break; }}}循环流程循环流程java中的3种循环流程方式1while循环2do….while循环3for循环while循环while(布尔表达式){
代码内容}while循环publicclassTestWhile{publicstaticvoidmain(Stringargs[]){ intcount=1; while(count<=5){ System.out.println("HelloWorld"+count); count++; }}}死循环死循环隐式死循环
while(true){ System.out.println("HelloWorld");}intcount=1;while(count<=5){ System.out.println("HelloWorld"+count);}do…while循环do…while循环do{
代码内容
}while(布尔表达式);publicclassTestDoWhile{publicstaticvoidmain(Stringargs[]){ intcount=1; do{ System.out.println("HelloWorld"+count); count++; }while(count<=5);}}while&do…while区别while&do…while的区别当不满足初始循环条件时,while的循环体一次都不执行
而do…while至少执行一次intcount=4;while(count<3){ System.out.println("HelloWorld");}intcount=4;do{ System.out.println("HelloWorld");}while(count<3);for循环for循环for(初始值;循环条件;增量表达式){
代码内容}
初始值即定义一个变量
循环条件即判断循环是否执行的条件
增量表达式即对先前定义变量进行加减运算for循环publicclassTestFor{publicstaticvoidmain(Stringargs[]){ for(inti=0;i<5;i++){ System.out.println("HelloWorld"+i); }}}for循环publicclassTestFor{publicstaticvoidmain(Stringargs[]){ for(inti=1;i<=5;i++){ System.out.println("HelloWorld"+i); }}}for循环规律for循环规律总结i初始值从0开始<n循环对应执行n次
i初始值从1开始<=n循环对应执行n次for循环体内i的取值依次从初始值开始一直取到条件判断的n值如果<n则取到n的前一个值,如果<=n取到n的值for(inti=0;i<5;i++){System.out.println("HelloWorld"+i);}for(inti=1;i<=5;i++){System.out.println("HelloWorld"+i);}for循环案例写一个程序:计算1…100的累加和
思路存储累计和的结果如何获得1...100的数字publicclassTestSum100{publicstaticvoidmain(Stringargs[]){
//定义一个int类型的变量存储1...100累加和的结果
intsum=0; for(inti=1;i<=100;i++){ sum+=i;//sum=sum+i; } System.out.println("1...100累计和的结果为
"+sum);}}for循环与while循环的使用场景for循环可以和while循环相互替换使用for循环一般多用于确定次数的循环while循环一般多用于不确定次数的循环break与continue关键字break关键字continue关键字for(inti=0;i<3;i++){ if(i==1)break;System.out.println("ivalueis"+i);}for(inti=0;i<3;i++){ if(i==1)continue; System.out.println("ivalueis"+i);}循环的标签循环的标号breakcontinue关键字和循环的标签lab:for(inti=0;i<3;i++){ System.out.println("ivalueis"+i);}lab:for(inti=0;i<3;i++){ if(i==1)breaklab; System.out.println("ivalueis"+i);}lab:for(inti=0;i<3;i++){ if(i==1)continuelab; System.out.println("ivalueis"+i);}循环嵌套循环嵌套for(inti=0;i<4;i++){for(intj=0;j<3;j++){
代码内容}}演示代码在下一页循环嵌套publicclassTestInnerFor{ publicstaticvoidmain(Stringargs[]){ for(inti=0;i<3;i++){ for(intj=0;j<2;j++){ System.out.println("i="+i+"j="+j); } }}循环嵌套案例写一个程序:输出如下形状思路内层循怎么控
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 首尔与北京气候的异同
- 盘点学生考试的试卷及答案
- 2025年瓯海美术真题试卷及答案
- 2025年科目2模拟测试题及答案
- 承接工程运输合同范本
- 安装清包工合同范本
- 数学老师有趣题库及答案
- 四川省2024-2025学年高二上学期期中调研测试历史试卷(含答案)
- 蔬菜酒店供货合同范本
- 项目承包模式合同范本
- 文化创业街区创意
- 年会合同协议书模板
- 中西医结合治疗类风湿关节炎疼痛
- 医疗人力资源效能评价指标体系构建
- 2025国际胰腺病学会急性胰腺炎修订指南解读课件
- 雨课堂学堂云在线《中国马克思主义与当代(北京化工大学 )》单元测试考核答案
- 贵州省贵阳市2025-2026学年高三上学期11月质量监测化学试卷(含答案)
- 机场设备维修与保养操作手册
- 动脉穿刺法教案(2025-2026学年)
- 2025年《肌肉骨骼康复学》期末考试复习参考题库(含答案)
- 工程勘察设计收费标准
评论
0/150
提交评论