Java语言基础(3选择结构).ppt

上传人:max****ui 文档编号:6354403 上传时间:2020-02-23 格式:PPT 页数:25 大小:263KB
返回 下载 相关 举报
Java语言基础(3选择结构).ppt_第1页
第1页 / 共25页
Java语言基础(3选择结构).ppt_第2页
第2页 / 共25页
Java语言基础(3选择结构).ppt_第3页
第3页 / 共25页
点击查看更多>>
资源描述
第二章Java语言基础 选择结构 新课引入 publicclassTest1 publicstaticvoidmain Stringargs intx y z x 1 y 2 z x y System out println x y z importjava io publicclassTest2 publicstaticvoidmain Stringargs throwsIOException intx y z Strings BufferedReaderr newBufferedReader newInputStreamReader System in System out println enterx s r readLine x Integer parseInt s 将该字符串转换为整型System out println entery s r readLine y Integer parseInt s 将该字符串转换为整型z x y System out println x y z 一 结构化编程 流程控制语句用于控制程序中各语句的执行顺序 Java提供的流程控制语句有顺序结构 选择结构 循环结构 转移等 if 条件表达式 语句块1 else 语句块2 二 if条件语句 说明 if后的表达式是判定条件 一般为逻辑表达式或关系表达式 语句中if和else属于同一个条件语句 else子句不能作为语句单独使用 必须与if配对使用 语句序列可以含一条或多条执行语句 当有多条执行语句时必须用 将几个语句括起来成为一条复合语句 否则 只执行第一条语句 else及后面的语句序列可以省略 在if子句末不能加分号 if与else的配对原则 二 if条件语句 例如 if a b System out println a b 二 if条件语句 例如 if score 60 System out println 你及格了 else System out println 你没有及格 二 if条件语句 例1 判断一个数是否为奇数 如果为奇数则输出 否则不予理睬 publicclassIsOdd publicstaticvoidmain String args intin 11 mod mod in 2 if mod 1 System out println 数字 in 为奇数 二 if条件语句 例2 要求在例1的执行过程中 如果用户输入偶数必须给出提示 二 if条件语句 if 条件表达式 语句块1 else语句块2 importjava io publicclassIsOdd2 publicstaticvoidmain Stringargs throwsIOException intx Strings BufferedReaderr newBufferedReader newInputStreamReader System in System out println enteraninteger s r readLine x Integer parseInt s if x 2 1 System out println 你输入的数字 x 为奇数 else System out println 你输入的数字 x 为偶数 例3 importjava io publicclassTest publicstaticvoidmain String args throwsIOException charch System out print 请输入一个字符 ch char System in read 从键盘输入一个字符if ch 0 二 if条件语句 if 条件1 语句块1 elseif 条件2 语句块2 elseif 条件3 语句块3 else语句块n if score 85 二 if elseif else多选择语句 if 表达式1 if 表达式2 语句序列1else语句序列2elseif 表达式3 语句序列3else语句序列4 二 if elseif else多选择语句 例如 if Score 60 System out println 不及格 elseif Score 80 System out println 及格 elseif Score 90 System out println 良好 elseSystem out println 优秀 二 if elseif else多选择语句 例4 根据年份和月份输出每月的天数 二 if elseif else多选择语句 publicclassIfTest publicstaticintdayOfMonth intyear intmonth if month 2 if year 4 0 elseif month 4 month 6 month 9 month 11 return30 else return31 publicstaticvoidmain String args intdays dayOfMonth 2008 2 System out println daysofFebruary 2008is days 二 if elseif else多选择语句 三 switch语句 switch 表达式 case值1 语句块1 case值2 语句块2 default 语句块n switch的常量和表达式可以是整型 字符型及byte型任何两个case常量值不可以有相同的值 只能对等式进行测试 即表达式的值是否等于值1 值2 根据表达式取值的不同转向不同的分支 每个case分支中的语句块无须用花括号括起来 每个case分支都只是入口点可在case语句块中加入break语句 可立即转出switch语句 不再走后面case流程 三 switch语句 例5 importjava io publicclassTest publicstaticvoidmain String args throwsIOException charch System out print 请输入成绩 字符 ch char System in read 从键盘输入一个字符switch ch case A System out println 85 100 break case B System out println 60 84 break case C System out println 0 59 break default System out println 输入有误 例6 importjava io publicclassTest publicstaticvoidmain String args throwsIOException charch System out print 请输入成绩 字符 ch char System in read 从键盘输入一个字符switch ch case a case A System out println 85 100 break case b case B System out println 60 84 break case c case C System out println 0 59 break default System out println 输入有误 练习 练习1 若a b c1 c2 x y均是整型变量 正确的switch语句是 A swich a b B switch a a b b case1 y a b break case3 case0 y a b break case1 y a b break case3 y b a break C switcha D switch a b case3 casec1 y a b break case4 x a b break casec2 x a d break case10 default x a b case11 y a b break default y a b break 练习2 分析程序运行结果 publicclasss1 publicstaticvoidmain String args intx 1 switch x case0 System out println first break case1 System out println second case2 System out println third break 练习3 分析程序运行结果 publicclassf1 publicstaticvoidmain String args intx 1 y 0 switch x case1 switch y case0 System out println first break case1 System out println second break case2 System out println third
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!