看前欣赏美图!
上才艺!
目录 一.顺序结构二.分支结构1.if语句2.switch 语句 三. 循环结构1 .while 循环2. break3. continue4.for循环5.do while循环 四.输入输出1.输出到控制台2 .从键盘输入 五.练习
一.顺序结构
顺序结构比较简单. 像我们之前写过的代码就是顺序结构的, 按照代码书写的顺序一行一行执行就可以了
System.out.println("1"); System.out.println("2"); System.out.println("3"); System.out.println("4"); //顺序结构`
结果:
二.分支结构
1.if语句
基本语法形式1
基本语法形式2
基本语法形式3
代码示例: 判定一个数字是奇数还是偶数
public static void main(String[] args) { int n = 10; if(n % 2 == 0){ //黄色框框是一个警告,计算机觉得它一定是真的 System.out.println("偶数"); } else{ System.out.println("奇数"); } }
结果:
2.switch 语句
基本语法:
switch(整数|枚举|字符|字符串){
case 内容1 : {
内容满足时执行语句;
[break;]
}
case 内容2 : {
内容满足时执行语句;
[break;]
}
…
default:{
内容都不满足时执行语句;
[break;]
}
}
代码示例: 根据 day 的值输出星期
public static void main(String[] args) { int day = 1; switch(day) { case 1: System.out.println("星期一"); break; case 2: System.out.println("星期二"); break; case 3: System.out.println("星期三"); break; case 4: System.out.println("星期四"); break; case 5: System.out.println("星期五"); break; case 6: System.out.println("星期六"); break; case 7: System.out.println("星期日"); break; default: System.out.println("输入有误"); break; } }
结果:
根据 switch 中值的不同, 会执行对应的 case 语句. 遇到 break 就会结束该 case 语句.
如果 switch 中的值没有匹配的 case, 就会执行 default 中的语句.
我们建议一个 switch 语句最好都要带上 default
注意事项1 break 不要遗漏, 否则会失去 [多分支选择] 的效果
注意事项2 switch 中的值只能是 整数|枚举|字符|字符串
注意事项3 switch 不能表达复杂的条件
注意事项4 switch 虽然支持嵌套, 但是很丑~
三. 循环结构
1 .while 循环
基本语法格式:
while(循环条件){
循环语句;
}
循环条件为 true, 则执行循环语句; 否则结束循环。
代码示例1: 计算 1 - 100的数字之和
public static void main(String[] args) { int n = 1; int result = 0; while (n <= 100) { result += n; n++; } System.out.println(result); }
结果:
代码示例2: 计算 1! + 2! + 3! + 4! + 5!
public static void main(String[] args) { int num = 1; int sum = 0; // 外层循环负责求阶乘的和 while (num <= 5) { int factorResult = 1; int tmp = 1; // 里层循环负责完成求阶乘的细节. while (tmp <= num) { factorResult *= tmp; tmp++; } sum += factorResult; num++; } System.out.println("sum = " + sum); }
结果:
这里我们发现, 当一个代码中带有多重循环的时候, 代码的复杂程度就大大提高了. 而比较复杂的代码就更容易出错.
后面我们会采用更简单的办法来解决这个问题
注意事项
和 if 类似, while 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { } 和 if 类似, while 后面的 { 建议和 while 写在同一行. 和 if 类似, while 后面不要多写 分号, 否则可能导致循环不能正确执行int num = 1; while (num <= 10); { System.out.println(num); num++; }
// 执行结果
[无任何输出, 程序死循环]
此时 ; 为 while 的语句体(这是一个空语句), 实际的 { } 部分和循环无关. 此时循环条件 num <= 10 恒成立, 导致代码死循环了.
2. break
break 的功能是让循环提前结束.
代码示例: 找到第一个 3 的倍数
//找到第一个被3整除的数 public static void main(String[] args) { int i = 1; while(i<=10){ if(i%3==0){ System.out.println(i); break; //结束所有的循环 } i++; } }
结果:
3. continue
continue 的功能是跳过这次循环, 立即进入下次循环.
代码示例: 找到 1-10中所有 3 的倍数
public static void main(String[] args) { int i = 1; while(i<=10){ if(i%3!=0){ i++; continue; //结束本趟循环 } System.out.println(i); i++; } }
结果:
4.for循环
基本语法
for(表达式1;表达式2;表达式3){
循环体;
}
表达式1: 用于初始化循环变量.
表达式2: 循环条件.
表达式3: 更新循环变量.
相比于 while 循环, for 循环将这三个部分合并在一起, 写代码时不容易遗漏.
**代码示例1:**计算1-100的和
public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { sum += i; } System.out.println(sum); }
结果:
代码示例2: 计算 5 的阶乘
public static void main(String[] args) { int result = 1; for (int i = 1; i <= 5; i++) { result *= i; } System.out.println("result = " + result); }
结果:
注意事项 (和while循环类似)
和 if 类似, for 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { } 和 if 类似, for 后面的 { 建议和 while 写在同一行. 和 if 类似, for 后面不要多写 分号, 否则可能导致循环不能正确执行5.do while循环
基本语法
do{
循环语句;
}while(循环条件);
先执行循环语句, 再判定循环条件
**代码示例:**打印1-10
public static void main(String[] args) { int num = 1; do { System.out.print(num+" "); num++; } while (num <= 10); }
结果:
有一点注意一下
public static void main21(String[] args) { int i=0; do{ System.out.println("haha"); }while(i!=0); //最少都要执行一次 }
它最少都要执行一次。
注意事项
do while 循环最后的分号不要忘记 一般 do while 很少用到, 更推荐使用 for 和 while.
四.输入输出
1.输出到控制台
基本语法
System.out.println(msg); // 输出一个字符串, 带换行
System.out.print(msg); // 输出一个字符串, 不带换行
System.out.printf(format, msg); // 格式化输出
println 输出的内容自带 \n, print 不带 \n
printf 的格式化输出方式和 C 语言的 printf 是基本一致的
代码示例:
public static void main(String[] args) { System.out.println("hello world"); System.out.print("hello world "); int x = 10; System.out.printf("x = %d\n", x); }
结果:
格式化字符串
没必要太用心记,用的时候查一下就知道了。
2 .从键盘输入
使用 Scanner 读取字符串/整数/浮点数
代码示例:
public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入你的姓名:"); String name = sc.nextLine(); System.out.println("请输入你的年龄:"); int age = sc.nextInt(); System.out.println("请输入你的工资:"); float salary = sc.nextFloat(); System.out.println("你的信息如下:"); System.out.println("姓名: "+name+"\n"+"年龄:"+age+"\n"+"工资:"+salary); sc.close(); }
结果:
五.练习
这里给大家做几个高频练习题:
1.根据年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
代码实现:
public static void main5(String[] args) { Scanner scanner = new Scanner(System.in); int Age = scanner.nextInt(); while(Age>0) { if ( Age <= 18) { System.out.println("少年"); break; } else if (Age > 18 && Age <= 28) { System.out.println("青年"); break; } else if (Age > 28 && Age <= 55) { System.out.println("中年"); break; } else { System.out.println("老年"); break; } } }
打印结果:
2. 判定一个数字是否是素数
代码实现:
public static void main4(String[] args) { Scanner scanner = new Scanner(System.in); int Num = scanner.nextInt(); int i; for (i = 2; i < Num; i++) { if (Num % i == 0) { System.out.println(Num + "不是素数"); break; } } if (Num==i){ System.out.println(Num+"是素数"); } }
打印结果:
3.输出乘法口诀表
代码实现:
public static void main(String[] args) { for (int i = 1; i <= 9 ; i++) { for (int j = 1; j <= i ; j++) { System.out.print(i+"*"+j+"="+i*j+" "); } System.out.println(); } }
打印结果:
4. 编写代码模拟三次密码输入的场景。 最多能输入三次密码,密码正确,提示[登录成功],密码错误, 可以重新输入,最多输入三次。三次均错,则提示退出程序
代码实现:
public static void login(){ Scanner scanner = new Scanner(System.in); int count = 3; while(count != 0){ System.out.println("请输入你的密码:"); String password = scanner.nextLine(); if(password.equals("123456")){ System.out.println("登陆成功了!"); break; }else{ count--; System.out.println("你输错密码了,你还有 "+count+"次机会!"); } } } public static void main16(String[] args) { login(); }
打印结果:
这里用到了方法,后面在说。
5.猜数字游戏
代码实现:
public static void main(String[] args) { Random random = new Random(); int rand = random.nextInt(100); Scanner scanner = new Scanner(System.in); while (true) { System.out.print("请输入你要猜的数字:"); int n = scanner.nextInt(); if (n < rand) { System.out.println("低了"); } else if (n > rand) { System.out.println("高了"); } else { System.out.println("猜对了"); break; } } }
打印结果:
都看到这里了,点个赞在走呗!
谢谢大家!
到此这篇关于Java程序中逻辑控制语句详解流程的文章就介绍到这了,更多相关Java 逻辑控制内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/chenbaifan/article/details/120883932
查看更多关于Java程序顺序结构中逻辑控制语句详解流程的详细内容...