云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > day03流程控制语句

day03流程控制语句

  • 62 次阅读
  • 3 次下载
  • 2025/6/14 20:48:47

1. 第二种格式与三元运算符的区别:三元运算符运算完要有值出现。好处是:可以写在其

他表达式中。

2. 条件表达式无论写成什么样子,只看最终的结构是否是true 或者 false。 练习1: 根据用户输入的月份,打印出月份所属的季节.

练习2: 根据用户输入的成绩,进行评级,根据学生考试成绩划分ABCD 练习1:

public static void main(String[] args) { int x = 1; if (x == 3) { System.out.println(\); } else if (x == 4) { System.out.println(\); } } 仔细观察:发现if和else if要执行的语句是一样的,可不可以合并呢。当然是可以的。怎么合并?使用逻辑运算符,那么使用哪个逻辑运算符呢, &肯定不行。需要全部为真才为真,月份是不可能同时满足的 那么使用|连接符号即可。意思只要其中一个为真,就为真。另外可以使用短路功能。

public static void main(String[] args) { int x = 1; if (x == 3 || x == 4 || x == 5) { System.out.println(\); } else if (x == 6 || x == 7 || x == 8) { System.out.println(\); } else if (x == 9 || x == 10 || x == 11) { System.out.println(\); } else { System.out.println(\); } else { System.out.println(\月份不存在\); } } 练习2: public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(\请输入考试分数:\); double score = sc.nextDouble(); char grade; if (score >= 90.0) grade = 'A'; else if (score >= 80.0) grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; System.out.println(\你的成绩是:\ + grade); }

If语句常见的错误:

1.忘记必要的括号:如果代码块中只有一条语句的时候,可以省略花括号,但是当花括号将多条语句扩在一起时,花括号就不能在省略。 double radius = 4; double area; if (radius >= 0) area = radius * radius * 3.14; System.out.println(\ + \ + area); double radius = 4; double area; if (radius >= 0) { area = radius * radius * 3.14; System.out.println(\ + \ + area); } 虽然代码一样多,但是第一个会编译报错(area没有出初始化),第二个正常运行。就是因为少了花括号。所以一定要仔细。 2.if语句后出现分号

double radius = 0; double area; if (radius > 0); { area = radius * radius * 3.14; System.out.println(\ + \ + area); } 注意:这是一个逻辑错误,编译和运行都不会报错,只是不会出现想要的结果。 相当于判断符合条件后,执行一个空语句。 double radius = 0; double area; if (radius > 0){}{ area = radius * radius * 3.14; System.out.println(\ + \ + area); } 判断闰年

1:什么是闰年?可以被4整除不能被100整除,或者可以被400整除,那么这一年就是闰年(leap year)

public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(\请输入年份:\); int year = sc.nextInt(); // 判断年份能否被4整除 boolean isLeapYear = (year % 4 == 0); // 年份能被4整除,并且不能被100整除并且使用&&(and) isLeapYear = isLeapYear && (year % 100 != 0); // 年份或者能够被400整除 isLeapYear = isLeapYear || (year % 400 == 0); if (isLeapYear) { System.out.println(year + \是闰年!\); } // 简写格式; if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + \是闰年!\); } }

3 选择判断语句(switch)

switch语句 格式: switch(表达式) { case 取值1: 执行语句; break; case 取值2: 执行语句; break; …... default: 执行语句; break; } switch语句特点:

1,switch语句选择的类型只有四种:byte,short,int , char。

2,case之间与default没有顺序。先判断所有的case,没有匹配的case执行 default。

3,switch语句停止的条件是遇到了break关键字或者结束switch语句的大括号。 4,如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运

行可以执行的语句,直到遇到break或者switch结尾结束。

5,switch case中的值必须要与switch表达式的值具有相同的数据类型。而且case后跟的值必须是常量,不能跟变量。 案例:

public static void main(String[] args) { int x = 3; switch (x) { case 1: System.out.println(\); break; System.out.println(\); break; case 2: case 3: } } System.out.println(\); break; System.out.println(\); break; default: case 就像选择题的答案之一。 break 就是如果该答案正确那么就可以跳出switch 了,意思就是说 已经找出了正确的答案了。那么这道题也就做完了。如果 case 没有匹配接着进行下一个case 匹配,直到匹配为止。 最后如果都没有匹配上,那么 switch 给提供了一个默认的答案,就是 default。 注意: case后跟的是冒号:

每个case中的执行语句一定要加break; 练习:

需求2:根据用于指定的月份,打印该月份所属的季节.

一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见break,利用这一特性可以让好几个case执行统一语句.

345 spring 678 sunmer 9 10 11 autumn 12 1 2 winter

public static void main(String[] args) { int x = 3; switch (x) { case 3: case 4: case 5: System.out.println(\); break; case 6: case 7: case 8: System.out.println(\); break; case 9: case 10: case 11: System.out.println(\); break; case 12: case 0: case 1: } System.out.println(\); System.out.println(\); break; default:

搜索更多关于: day03流程控制语句 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

1. 第二种格式与三元运算符的区别:三元运算符运算完要有值出现。好处是:可以写在其他表达式中。 2. 条件表达式无论写成什么样子,只看最终的结构是否是true 或者 false。 练习1: 根据用户输入的月份,打印出月份所属的季节. 练习2: 根据用户输入的成绩,进行评级,根据学生考试成绩划分ABCD 练习1: public static void main(String[] args) { int x = 1; if (x == 3) { System.out.println(\); } else if (x == 4) { System.out.println(\); } } 仔细观察:发现if和else if要执行的语句是一样的,可不可以合并呢。当然是可以的。怎么合并?使用逻辑运算符,

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com