Java基础知识-流程控制
分支结构
if分支
- if分支的写法有几种,各有什么特点?
- 案例
package com.itheima.branch;
/**
* 目标:学会使用if分支结构解决问题,理解其过程
*/
public class ifDemo1 {
//主方法
public static void main(String[] args) {
// 需求:心跳(60-100)之间是正常的,否则系统提示进一步检查
// 格式1:if(条件表达式){代码...}
int hearBeat = 90;
if (hearBeat < 60 || hearBeat > 100) {
System.out.println("您的心跳数据是:" + hearBeat + ",您可能需要进一步检查!");
}
System.out.println("检查结束");
// 格式2:if(条件表达式){ 代码...}else{ 代码...}
// 需求:发红包
double money =5999;
// 发送一个1314红包
if (money >= 1314) {
System.out.println("您的红包在发送中");
} else {
System.out.println("您自己都没有钱,别发了");
}
// 格式3: if(条件表达式) { 代码...} else if { 条件表达式} {代码...}...else { 代码...}
// 绩效系统:0-60 C 60-80 B 80-90 A 90-100 A+
int score = 99;
if (score >=0 && score <60) {
System.out.println("你本月的绩效是:C");
} else if (score >= 60 && score < 80) {
System.out.println("你本月的绩效是:B");
} else if (score >= 80 && score < 90) {
System.out.println("你本月的绩效是:A");
} else if (score >= 90 && score <= 100) {
System.out.println("你本月的绩效是:A+");
}
}
}
switch分支
-
switch分支的格式,执行流程是怎么样的?
switch(表达式) {
case 值1:
执行代码1;
break;
case 值2:
执行代码2;
break;
…
case 值n-1:
执行代码n-1;
break;
default:
执行代码n;
} -
if,switch分支各自适合做什么业务场景?
if其实在功能上远远强大于switch。
if适合做区间匹配。
switch适合做:值匹配的分支选择,代码优雅。 -
switch分支注意事项:
-
案例
package com.itheima.branch;
public class SwitchDemo2 {
//主方法
public static void main(String[] args) {
// 目标:学会使用swithc分支结构,理解流程
String weekday = "周三";
switch (weekday) {
case "周一":
System.out.println("埋头苦干,解决bug");
break;
case "周二":
System.out.println("调试程序");
break;
case "周三":
System.out.println("去吃烧烤");
break;
case "周四":
System.out.println("主动帮助别人解决bug");
break;
case "周五":
System.out.println("今晚吃鸡");
break;
case "周六":
System.out.println("与人相约看电影");
break;
case "周日":
System.out.println("继续努力工作");
break;
default:
System.out.println("数据有误!");
}
}
}
switch穿透现象
- 什么情况下会出现switch穿透现象?
case中没有写break - switch穿透性能解决什么问题?
存在多个case分支的功能代码是一样时,可以用穿透性把流程集中到同一处处理,这样可以简化代码 - 案例
package com.itheima.branch;
public class SwitchDemo4 {
//主方法
public static void main(String[] args) {
// 需求:用户输入月份可以展示该月份的天数
// 1,3,5,7,8,10,12月份是31天
// 2月份是闰年为29天,非闰年为28天
// 4,6,9,11月份是30天
int month = 7;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(month + "是31天!");
break;
case 2:
System.out.println(month + "月闰年为29天,非闰年为28天!");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(month + "是30天!");
break;
default:
System.out.println("数据有误!");
}
}
}
循环结构
for循环
- for循环格式和执行流程是什么样的?
- 案例1
如何实现元素求1-5的和?
package com.itheima.loop;
public class ForTest2 {
//主方法
public static void main(String[] args) {
// 需求:求1-5之间的数据和,并把求和结果在控制台输出。
// 2.定义一个整数变量用于累加数据求和
int sum = 0;
// 1.定义一个for循环找到1 2 3 4 5
for (int i = 1; i <= 5 ; i++) {
// i = 1 2 3 4 5
// 3. 把循环的数据累加给sum变量
/**
* 等价于:sum = sum + 1
* i == 1 sum = 0 + 1
* i == 2 sum = 1 + 2
* i == 3 sum = 3 + 3
* i == 4 sum = 6 + 4
* i == 5 sum = 10 + 5
*/
sum += i;
}
System.out.println("1-5的和是:" + sum);
}
}
- 案例2
求1-10之间的奇数和,并把求和结果在控制台输出
方式一:在for循环中,通过if筛选出奇数
方式二:直接使用for循环找出奇数
package com.itheima.loop;
public class ForTest3 {
//主方法
public static void main(String[] args) {
// 需求:求1-10的奇数和
// 3.定义一个求和的变量 累加奇数和
int sum = 0;
// 1. 定义一个循环找到1 2 3 4... 10
for (int i = 1; i <= 10; i++) {
// i 1 2 3 4 5 6 7 8 9 10
// 2. 筛选出奇数
if (i % 2 == 1) {
// i = 1 3 5 7 9
sum += i;
}
}
// 4. 输出求和变量即可
System.out.println("1-10的奇数和是:" + sum);
System.out.println("--------------------------");
// 1. 定义循环找到1 3 5 7 9
// 2.定义一个求和的变量,累加奇数和
int sum1 = 0;
for (int i = 1; i <= 10; i += 2) {
// i = 1 3 5 7 9
sum1 += i;
}
// 3. 输出求和变量即可
System.out.println("1-10的奇数和是:" + sum1);
}
}
- 案例3
求水仙花数
package com.itheima.loop;
public class ForTest4 {
//主方法
public static void main(String[] args) {
// 需求:找出水仙花数
// 在循环外定义一个变量用于记录水仙花的个数
int count = 0;
// 1. 定义一个for循环找出全部三位数
for (int i = 100; i <= 999; i++) {
//2. 判断这个三位数是否满足要求
// i = 153
// 个位
int ge = i % 10;
// 十位
int shi = i / 10 % 10;
// 百位
int bai = i / 100;
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
System.out.print(i + "\t");
count++;
}
}
System.out.println();
System.out.println("水仙花个数是:" + count);
}
}
while循环
- while循环的格式,执行流程是怎么样的?
2.什么时候用for,什么时候用while?
3.案例
package com.itheima.loop;
public class WhileTest6 {
//主方法
public static void main(String[] args) {
// 需求:珠穆朗玛峰高度是8848860 纸张厚度 0.1 折叠纸张直到不低于珠穆朗玛峰高度,求折叠几次
// 1.定义变量记录山峰的高度,纸张的厚度
double peakHeight = 8848860;
double paperHeight = 0.1;
// 3. 定义一个变量用于记录纸张折叠次数
int count = 0;
// 2. 定义一个while循环控制纸张进行折叠
while (paperHeight < peakHeight) {
// 让纸张的厚度多一倍
paperHeight *= 2;
count++;
}
System.out.println("折叠的次数" + count);
System.out.println("纸张的最终厚度," + paperHeight);
}
}
do-while循环
三种循环的区别
死循环
嵌套循环
跳转关键字:break,continue
案例技术:随机数Random类
案例
package com.itheima.random;
import java.util.Random;
public class RandomDemo1 {
//主方法
public static void main(String[] args) {
// 目标:学会使用java提供1的随机数类Random
// 1. 导包
// 2. 创建随机数对象
Random r = new Random();
// 3. 调用nextInt功能(方法)可以返回一个整数的随机数给你
for (int i = 0; i < 20; i++) {
int data = r.nextInt(11);
System.out.println(data);
}
System.out.println("-------------------------");
// 1 - 10 ===> -1 ===> (0-9) + 1
int data = r.nextInt(10) + 1;
System.out.println(data);
}
}
猜数字游戏
package com.itheima.random;
import java.util.Random;
import java.util.Scanner;
public class RandomTest2 {
//主方法
public static void main(String[] args) {
// 1.随机一个幸运号码1-100之间
Random r = new Random();
int luckNumber = r.nextInt(100) + 1;
// 2. 使用一个死循环让用户不断的去猜测,并给提示
Scanner sc = new Scanner(System.in);
while (true) {
// 让用户输入猜测数据
System.out.println("请输入猜测数据(1-100):");
int guessNumber = sc.nextInt();
// 3. 判断这个猜测的号码与幸运号码的大小情况
if (guessNumber > luckNumber) {
System.out.println("你的猜测过大");
} else if (guessNumber < luckNumber) {
System.out.println("你的猜测过小");
} else {
System.out.println("恭喜你,猜中了");
break;
}
}
}
}