1、day03流程控制语句1 顺序语句语句:使用分号分隔的代码称作为一个语句。注意:没有写任何代码只是一个分号的时候,也是一条语句,称作空语句。顺序语句就是按照从上往下的顺序执行的语句。2 判断(ifelse) 在我们找工作的过程中,要求两年工作经验以上且年龄超过30岁。 什么是判断语句:用于判断的语句叫判断语句。1.格式一 if(判断条件) 如果符合条件执行的代码; 执行的代码块1; 执行的代码块2; .; 执行的代码块n;练习:提示用户输入一个整数。如果该整数是5的倍数,打印“5的倍数”如果是2的倍数打印“2的倍数”提示:为了便于让用户输入数据,我们使用Scanner这个类,固定用法Scann
2、er sc=new Scanner(System.in); 该类需要导入包import java.util.Scanner;int nextInt = sc.nextInt();获取用户输入的数字import java.util.Scanner;publicclass Demo9 publicstaticvoid main(String args) Scanner sc=new Scanner(System.in); int nextInt = sc.nextInt(); if(nextInt%5=0) System.out.println(是5的倍数); if(nextInt%2=0) Sy
3、stem.out.println(是2的倍数); 2.格式二if(判断条件) 执行的代码块1; 执行的代码块2; .; 执行的代码块n;else 执行的代码块1; 执行的代码块2; .; 执行的代码块n; 案例:判断一个整数是奇数还是偶数publicstaticvoid main(String args) Scanner sc = new Scanner(System.in); System.out.println(请输入一个整数:); int nextInt = sc.nextInt(); if (nextInt % 2 = 0) System.out.println(是偶数); else
4、System.out.println(是奇数); System.out.println(over); 同样道理如果花括号中只有一条语句,那么花括号可以省略不写,初学者不推荐省略。publicstaticvoid main(String args) Scanner sc = new Scanner(System.in); System.out.println(请输入一个整数:); int nextInt = sc.nextInt(); if (nextInt % 2 = 0) System.out.println(是偶数); else System.out.println(是奇数); Syste
5、m.out.println(over); 观察发现if else语句有点类似于三元运算符.其实三元运算符是if else 的一种简写格式.Publicstaticvoid main(String args) int x = 0, y = 1, b; / if else 语句 if (x y) b = x; else b = y; System.out.println(b);/ 1 / 3元运算 b = x y ? x : y; System.out.println(b); / 1这两种格式是一样的。if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2;三元运算符:好处
6、:可以简化if else代码。弊端:因为是一个运算符,所以运算完必须要有一个结果。3 格式三if(判断条件1) 执行的代码块1;else if(判断条件2) 执行语句;else if(判断条件3) 执行语句;需求: 根据用户定义的数值不同,打印对应的星期英文。if 只能进行一层判断,if else 只能进行两层判断,那么需要多层判断时呢?星期可是有7个数的。如何设计代码?使用if 语句publicstaticvoid main(String args) int x = 8; if (x = 1) System.out.println(星期一); if (x = 2) System.out.pr
7、intln(星期二); if (x = 3) System.out.println(星期三); 如果这样设计的话,第一个if语句执行完毕后,第二个语句仍会执行(去判断),是一个顺序结构.那么事实上当前定义的星期之后会有一个.假如,第一个已经符合条件,那么剩余的执行就没有意义了。属于逻辑错误。使用if else ,如果用户输入的是7以外的数据,那么怎么处理?就需要使用else 了方案2:使用if else if语句publicstaticvoid main(String args) int x = 8; if (x = 1) System.out.println(星期一); elseif (x
8、= 2) System.out.println(星期二); elseif (x = 3) System.out.println(星期三); elseif (x = 4) System.out.println(星期四); elseif (x = 5) System.out.println(星期五); elseif (x = 6) System.out.println(星期六); elseif (x = 7) System.out.println(星期日); else System.out.println(请输入数字1-7); 注意:publicstaticvoid main(String arg
9、s) int x = 5; if (x = 1) System.out.println(1); if (x = 2) System.out.println(2); if (x = 3) System.out.println(3); else System.out.println(4); / 4 该if 语句不是一个整体,第一个if 是一个语句,第二个又是一个语句,最后的if else 又是一个语句。if语句特点1. 第二种格式与三元运算符的区别:三元运算符运算完要有值出现。好处是:可以写在其他表达式中。2. 条件表达式无论写成什么样子,只看最终的结构是否是true 或者 false。练习1:
10、根据用户输入的月份,打印出月份所属的季节.练习2: 根据用户输入的成绩,进行评级,根据学生考试成绩划分ABCD 练习1:publicstaticvoid main(String args) int x = 1; if (x = 3) System.out.println(spring); elseif (x = 4) System.out.println(spring); 仔细观察:发现if和else if要执行的语句是一样的,可不可以合并呢。当然是可以的。怎么合并?使用逻辑运算符,那么使用哪个逻辑运算符呢, &肯定不行。需要全部为真才为真,月份是不可能同时满足的 那么使用|连接符号即可。意思
11、只要其中一个为真,就为真。另外可以使用短路功能。publicstaticvoid main(String args) int x = 1; if (x = 3 | x = 4 | x = 5) System.out.println(spring); elseif (x = 6 | x = 7 | x = 8) System.out.println(Summer); elseif (x = 9 | x = 10 | x = 11) System.out.println(autumn); else System.out.println(Winter); else System.out.printl
12、n(月份不存在); 练习2:publicstaticvoidmain(String args) Scanner sc = new Scanner(System.in); System.out.println(请输入考试分数:); double score = sc.nextDouble(); char grade; if (score = 90.0) grade = A; elseif (score = 80.0) grade = B; elseif (score = 70.0) grade = C; elseif (score = 60.0) grade = D; else grade =
13、F; System.out.println(你的成绩是: + grade); If语句常见的错误:1忘记必要的括号:如果代码块中只有一条语句的时候,可以省略花括号,但是当花括号将多条语句扩在一起时,花括号就不能在省略。double radius = 4; doublearea; if (radius = 0) area = radius * radius * 3.14; System.out.println(The area + is + area);double radius = 4; double area; if (radius = 0) area = radius * radius *
14、 3.14; System.out.println(The area + is + area); 虽然代码一样多,但是第一个会编译报错(area没有出初始化),第二个正常运行。就是因为少了花括号。所以一定要仔细。2if语句后出现分号double radius = 0; double area; if (radius 0); area = radius * radius * 3.14; System.out.println(The area + is + area); 注意:这是一个逻辑错误,编译和运行都不会报错,只是不会出现想要的结果。相当于判断符合条件后,执行一个空语句。double rad
15、ius = 0; double area; if (radius 0) area = radius * radius * 3.14; System.out.println(The area + is + area); 判断闰年1:什么是闰年?可以被4整除不能被100整除,或者可以被400整除,那么这一年就是闰年(leap year)publicstaticvoidmain(String args) Scanner sc = new Scanner(System.in); System.out.println(请输入年份:); int year = sc.nextInt(); / 判断年份能否被
16、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.pri
17、ntln(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没有
18、对应的break,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾结束。 5,switch case中的值必须要与switch表达式的值具有相同的数据类型。而且case后跟的值必须是常量,不能跟变量。案例:public static void main(String args) int x = 3; switch (x) case 1: System.out.println(1); break; case 2: System.out.println(2); break; case 3: System.out.println(3); break; default
19、: System.out.println(ok); break; case 就像选择题的答案之一。 break 就是如果该答案正确那么就可以跳出switch 了,意思就是说 已经找出了正确的答案了。那么这道题也就做完了。如果 case 没有匹配接着进行下一个case 匹配,直到匹配为止。 最后如果都没有匹配上,那么 switch 给提供了一个默认的答案,就是 default。注意: case后跟的是冒号:每个case中的执行语句一定要加break; 练习:需求2:根据用于指定的月份,打印该月份所属的季节.一旦case匹配,就会顺序执行后面的程序代码,而不管后面的case是否匹配,直到遇见bre
20、ak,利用这一特性可以让好几个case执行统一语句.345 spring 678 sunmer 9 10 11 autumn 12 1 2 winterpublic static void main(String args) int x = 3; switch (x) case 3: case 4: case 5: System.out.println(spring); break; case 6: case 7: case 8: System.out.println(sunmer); break; case 9: case 10: case 11: System.out.println(au
21、tumn); break; case 12: case 0: case 1: System.out.println(winter); default: System.out.println(ok); break; 练习:char 类型在switch 中的使用.public static void main(String args) int x = 1, y = 2; char ch = *; switch (ch) case +: System.out.println(x*y= + (x + y); break; case -: System.out.println(x-y=+(x-y); b
22、reak; case *: System.out.println(x*y=+(x*y); break; case /: System.out.println(x/y=+(x/y); break; default: System.out.println(不靠谱); if 和switch 语句很像,具体什么场景下,应用哪个语句呢?如果判断的具体数值不多,而是符号byte,short int char 四种类型.虽然2个语句都可以使用,建议使用switch语句.因为效率稍高.其他情况:对区间判断,对结果为boolean 类型判断,使用if if的使用范围更广。if 除了能判断具体数值还能判断区间。s
23、witch 判断区间会很费劲的。要写好多case 对于运算结果是boolean型的 if 能判断 switch 是不能实现的。例如:根据学生考试成绩划分ABCD A90-100 B80-89 C70-79 D60-69 E0-59。实际开发怎么选择呢? 如果要对具体数值进行判断,并且数值不多,那么 就用switch 来完成。switch的case条件都是编译期整数常量,编译器可以做到表格跳转查询,查找速度快。但是switch 的局限性比较大必须是4种类型,并且值不多。一般都是使用if。 最后在jdk 7中对switch 进行了增强 还可以判断字符串。5.0 增加了对枚举的判断。备注:JDK7.
24、0开始可以使用switch可以使用字符串类型的数据了.4 While循环需求:需要打印一行字符串hello gzitcast,100次就需要将该语句打印100遍System.out.println(hello gzitcast);那么如何解决该问题? Java提供个一个称之为循环的结构,用来控制一个操作的重复执行。int count = 0; while (count 100) System.out.println(hello gzitcast); count+;System.out.println(over);变量count初始化值为0,循环检查count100 是否为true,如果为tru
25、e执行循环体(while后之间的语句),输出hello gzitcast语句,然后count自增一,重复循环,直到count是100时,也就是count100为false时,循环停止。执行循环之后的下一条语句。 Java提供了三种类型的循环语句:while循环,do-while循环和for循环。1、while语句格式:while(条件表达式) 执行语句;定义需求: 想要打印5次helloworldpublic static void main(String args) System.out.println(hello world); System.out.println(hello world
26、); System.out.println(hello world); System.out.println(hello world); System.out.println(hello world);2、public static void main(String args) int x = 0; while (x 5) System.out.println(hello java ); 如果是在dos里编译和运行,是不会停止,除非系统死机。需要ctrl+c来结束。这就是真循环或者死循环。因为x5 永远为真。public static void main(String args) int x
27、= 0; while (x 5) System.out.println(hello java ); x+; 让x自增,那么就会有不满足条件的时候。循环就会结束。练习:想要打印出1-100之间的奇数public static void main(String args) int x = 1; while (x 100) System.out.println(x); x = x + 2; public static void main(String args) int x=1; while(x100) if(x%2!=0) System.out.print(x); x+; System.out.println(); 练习2:计算1+2+3+4+5+6+7+8+9 的值int sum = 0; int i = 1; while (i 10) su
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1