流程控制
if流程概念
|
package com.cloud.day2; /* 控制流程语句之(if)判断语句 if判断语句的格式: 格式1:适用于一种情况使用。 if(判断的条件){ 符合条件执行的代码; } 格式2 : 适用于两种情况下去使用的。 if(判断条件){ 符合条件执行的代码 }else{ 不符合条件执行的代码; }
三元运算符的格式:布尔表达式?值1:值2; if-else 与三元运算符非常像: 三元运算符的优点:结构比较简洁。 三元运算符的缺点:符合条件必须要返回一个结果,不能执行语句。 格式三:适用于多种情况下去使用的。 if(判断条件1){ 符合条件1执行的代码 }else if(判断条件2){ 符合条件2执行的代码 }else if(判断条件3){ 符合条件3执行的代码 }......else{ 都不符合上述的条件执行的代码 } if语句要注意的细节: 1. 如果符合条件后只有一个语句需要执行,那么可以省略大括号。但是建议不要省略,因为结构不清晰。 2. if语句的判断条件后不能添加分号,否则会影响到执行的效果的, 需求1:工作经验要两年或者两年以上。 需求2:根据一个变量所记录的数字输出对应的星期。 0 ---星期天 1 星期一 */ public class Demo1 { public static void main(String[] args) { int num = 31;
if(num==0){ System.out.println("星期天"); }else if(num==1){ System.out.println("星期一"); }else if(num==2){ System.out.println("星期二"); }else if(num==3){ System.out.println("星期三"); }else if(num==4){ System.out.println("星期四"); }else if(num==5){ System.out.println("星期五"); }else if(num==6){ System.out.println("星期六"); }else{ System.out.println("没有对应的星期"); } } } |
if条件判断
|
package com.cloud.day2; import java.util.Scanner; /* 需求:键盘录入一个分数,根据分数输出对应的等级。 比如: 100-90 A等级 89-80 B等级 ..... E等级 接受键盘录入数据的步骤: 1. 创建一个扫描器对象。 2. 调用扫描器对象的nextInt方法扫描数据。 3. 导入包。 */ public class Demo2 { public static void main(String[] args) { //创建一个扫描器 Scanner scanner = new Scanner(System.in); System.out.println("请输入一个分数..."); int score = scanner.nextInt(); if(score>=90&&score<=100){ System.out.println("A等级"); }else if(score>=80&&score<=89){ System.out.println("B等级"); }else if(score>=70&&score<=79){ System.out.println("C等级"); }else if(score>=60&&score<=69){ System.out.println("D等级"); }else if(score>=0&&score<=59){ System.out.println("E等级"); }else{ System.out.println("补考.."); } } } |
switch判断
|
package com.cloud.day2; /* 控制流程语句之----switch选择判断语句 switch语句的格式: switch(你的选择){ case 值1: 符合值1执行的代码 break; case 值2: 符合值 2执行的代码 break; case 值3: 符合值 3执行的代码 break; case 值4: 符合值 4执行的代码 break; ...... default: 你的选择都不符合上述的选项时执行的代码; break; } switch语句要注意的事项: 1. switch语句使用的变量只能是byte、 char、 short、int、 String数据类型,String数据类型是从jdk7.0的时候开始支持的。 2. case后面跟的数据必须是一个常量。 3. switch的停止条件: switch语句一旦匹配上了其中的一个case语句,那么就会执行对应的case中的语句代码,执行完毕之后如果没有 遇到break关键字或者是结束switch语句的大括号,那么switch语句不会再判断,按照代码的顺序从上往下执行 所有的代码。直到遇到break或者是结束siwitch语句的大括号为止。 4. 在switch语句中不管代码的顺序如何,永远都是会先判断case语句,然后没有符合的情况下才会执行default语句。 if--else if---else if 语句与switch语句非常的相似: switch语句的优点:switch语句的结构清晰。 switch缺点:如果判断的条件是一个区间范围的,使用switch操作就非常的麻烦了。 判断以下那些不是计算机语言( D ) A java B C# C javascript D android */ class Demo3{ public static void main(String[] args) { int option = 1; //定义一个变量存储你的选择 switch(option){ case 1: System.out.println("java"); case 2: System.out.println("C#"); case 3: System.out.println("javascript"); break; case 4: System.out.println("android"); default: System.out.println("你的选择有误"); } } } |
switch判断月份
|
package com.cloud.day2; /* 需求:接受键盘录入一个月份,根据对应的月份输出对应的季节。 345 春天 678 夏天 9 10 11 秋天 1 2 12 冬天 要求使用switch语句实现。 */ import java.util.*; class Demo4 { public static void main(String[] args) { System.out.println("请输入一个月份:"); //创建一个扫描器 Scanner scanner = new Scanner(System.in); //调用扫描器的nextInt方法 int month = scanner.nextInt(); switch(month){ 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 1: case 2: System.out.println("冬天"); break; default: System.out.println("没有对应的季节"); break; } } } |
while循环
|
package com.cloud.day2; /* 循环语句----while循环语句 while循环语句的格式: while(循环的条件){ 循环语句; } while循环语句要注意的事项: 1. while循环语句一般是通过一个变量控制其循环的次数。 2. while循环语句的循环体代码如果只有一个语句的时候,那么可以省略大括号。但是也是不建议大家省略。 3. while循环语句的判断条件后面不能跟有分号,否则会影响到执行的效果。 需求:在控制上打印五句hello world. */ class Demo5 { public static void main(String[] args) { int count = 0; while(count<5){ System.out.println("Hello World!"); count++; } } } |
while求和
|
package com.cloud.day2; public class Demo6 { /** * while求1+2+...+100的和 */ public static void main(String[] args) { int i=1; int sum = 0; while(i<=100){ sum+=i; i++; } System.out.println(sum); } } |
while求次数
|
package com.cloud.day2; public class Demo7 { /** * 需求1:计算1-100,7的倍数总和。 7 14 21 */ public static void main(String[] args) { int i = 1; int sum = 0; while(i<=100){ if(i%7==0){ sum+=1; } i++; } System.out.println(sum); } } |
while+if的猜数字游戏
|
package com.cloud.day2; import java.util.Random; import java.util.Scanner; /* 需求2:实现猜数字游戏,如果没有猜对可以继续输入你猜的数字,如果猜对了停止程序。 最多只能猜三次,如果还剩下最后一次机会的时候要提醒用户。 */ public class Demo8 { public static void main(String[] args) { //随机生成一个数 Random random = new Random(); int ranNum = random.nextInt(10)+1;//1-10的随机数 //限制猜的次数 int count = 3; Scanner scanner = new Scanner(System.in); while(true){ System.out.println("请输入你猜的数字..."); int num = scanner.nextInt(); if(num>ranNum){ System.out.println("你猜的数字大了"); }else if(num<ranNum){ System.out.println("你猜的数字小了"); }else{ System.out.println("猜中了..."); } count--; if(count==1){ System.out.println("你还有1次机会"); } if(count==0){ break; } } } } |
Do…While循环
|
package com.cloud.day2; /* 控制流程语句----do while循环语句 格式: do{ }while(判断条件); 需求:在控制上打印五句hello world. while循环语句与do-while循环语句的区别: while循环语句是先判断后执行循环语句的,do-while循环语句 是先执行,后判断。不管条件是否满足至少会执行一次。 */ class Demo9{ public static void main(String[] args) { int count = 0; do { System.out.println("hello world"); count++; }while(count<5); } } |
Do…while求和
|
package com.cloud.day2; /* 需求:使用do-while算出1-100之间偶数的总和。 */ public class Demo10 { public static void main(String[] args) { int i = 1; int sum = 0; do { if(i%2==0){ sum+=i; } i++; } while (i<=100); System.out.println("sum="+sum); } } |
For循环
|
package com.cloud.day2; /* 控制流程语句之---for循环语句 for循环语句的格式: for(初始化语句;判断语句;循环后的语句){ 循环语句; }
for循环语句要注意的事项: 1. for(;;)这种写法是一个死循环语句,相当于while(true); 2. for循环语句的初始化语句只会执行一次,只是在第一次循环的时候执行而已。 3. for循环语句的循环体语句只有一句的时候,可以省略大括号不写。但是不建议省略。 需求:在控制上打印五句hello world. */ public class Demo11 { public static void main(String[] args) { for(int count = 0 ; count<5; count++){ System.out.println("hello world:"+count); } { System.out.println("hello world"); } } } |
For循环打印正三角形
|
package com.cloud.day2; /* 需求:在控制台上打印一个正立的直角三角形。 * ** *** **** ***** 多行多列的图形。 行数 5行 列数:会发生变化的. 分析列数: i = 0 ; i<5; j=0 ; j<=i 1个星号 i = 1 ; i<5 ;j=0 ; j<=1 2个星号 i = 2 ; i<5; j=0 ; j<=2 3个星号 ..... */ public class Demo12 { public static void main(String[] args) { //行数 for(int i=0;i<=5;i++){ //每行数 for(int j=0;j<=i;j++){ System.out.print("*"); } System.out.println(); } } } |
for循环打印倒三角形
|
package com.cloud.day2; /* 需求:打印一个倒立的直角三角形。 ***** **** *** ** * 5行 列数会发生变化 j<(5-i) i= 0 ; i<5; j=0 ; j<5 ; 五个星号 i = 1; i<5; j=0 ; j<4; 四个星号 i = 2; i<5; j=0 ; j<3; 三个星号 */ public class Demo13 { public static void main(String[] args) { for(int i=5;i>0;i--){ for(int j=1;j<=i;j++){ System.out.print("*"); } System.out.println(); } } } |
九九乘法表
|
package com.cloud.day2; /** * 打印99乘法表 */ public class Demo14 { public static void main(String[] args) { for(int i=1;i<=9;i++){ for(int j=1;j<=i;j++){ System.out.print(j+"*"+i+"="+i*j+" "); } System.out.println(); } } } |
转义字符
|
package com.cloud.day2; /* 转义字符:特殊字符使用”\”把其转化成字符的本身输出,那么使用”\”的字符称作为转移字符。 需求:在控制台上打印一个 hello" world 常见的转义字符有: \b Backspace (退格键) \t Tab 制表符(制表符的作用就是为了让一列对齐) 一个tab一般等于四个空格。 \n 换行 \r 回车 把光标移动到一行的首位置上。 注意:如果是在windows系统上操作文件的时候需要换行,是需要\r\n一起使用的。 如果是在其他的操作系统上需要换行,仅需要\n即可。 */ public class Demo15{ public static void main(String[] args) { System.out.println("hello\nworld"); System.out.println("\thello"); System.out.println("\rnihao"); } } |
break关键字
|
package com.cloud.day2; /* break、 break适用范围:只能用于switch或者是循环语句中。 break作用: 1. break用于switch语句的作用是结束一个switch语句。 2. break用于循环语句中的作用是结束当前所在的循环语句。
笔试题目:break目前位于内层的for循环,如何才能让break作用于外层的for循环。 可以标记解决
标记的命名只要符合标识符的命名规则即可。 */ public class Demo16 { public static void main(String[] args) { a:for(int i=0;i<3;i++){ System.out.println("外层"); b:for(int j=1;j<3;j++){ break b; } } } } |
continue关键字
|
package com.cloud.day2; /* continue关键字 continue的适用范围: continue只能用于循环语句。 continue的作用:continue的作用是跳过本次的循环体内容。继续下一次。 continue要注意的事项: 1. 在一种情况下,continue后面不能跟有其他语句,因为是永远都无法执行到。 2. continue 也可以配合标记使用的。 */ public class Demo17 { public static void main(String[] args) { //求1-100的偶数和 int sum = 0; for(int i=1;i<=100;i++){ if(i%2!=0){ continue; } sum+=i; } System.out.println("sum="+sum); } } |
本文深入讲解Java中的流程控制语句,包括if、switch、while、do-while及for循环等,详细介绍了各种语句的语法格式、注意事项及应用场景,并通过实例帮助理解。
2469

被折叠的 条评论
为什么被折叠?



