前言
文章内容输出来源:拉勾教育JAVA就业训练营
总目录跳转链接:总目录
如有侵权请联系本人
邮箱:xiaogueika@tom.com
一、分支结构(重中之重)
1.分支结构的概念
- 当需要进行条件判断并做出选择时,使用分支结构
2.if分支结构的概念和使用
1.if分支结构:
if([条件表达式]) {
[语句块;]
}
- 判断条件表达式是否成立
=> 若成立,则执行语句块;
=> 若不成立,则跳过语句块;
package cn.cyh.flow;
import java.util.Scanner;
/**
* 骗程使用if分支结构模拟网吧上网的过程
* @author cyh
* @create 2020/12/11 8:47
*/
public class IfTest {
public static void main(String[] args) {
//1.提示用户输入年龄信息并使用变量记录
System.out.println("欢迎光临");
System.out.print("请输入你的年龄:");
Scanner scanner =new Scanner(System.in);
int age = scanner.nextInt();
//2.使用if分支结构判断是否成年并给出对应的提示
if(age >= 18){
System.out.println(age + "岁成年了,允许上网");
} else {
System.out.println(age + "岁未成年,禁止上网");
}
//3.打印一句话
System.out.println("欢迎下次光临!");
}
}
案例题目
提示用户输入两个整数,使用
if
分支结构找到最大值并打印出来。
package cn.cyh.flow;
import java.util.Scanner;
/**
* 提示用户输入两个整数,使用if分支结构找到最大值并打印出来。
* @author cyh
* @create 2020/12/11 8:56
*/
public class IfMaxTest {
public static void main(String[] args) {
//1.提示用户输入两个整数并使用变量记录
System.out.println("请输入两个整数:");
Scanner scanner =new Scanner(System.in);
int ia = scanner.nextInt();
int ib = scanner.nextInt();
//使用if分支结构找到最大值并打印出来。
//方式一:
if(ia >= ib){
System.out.println("最大值是: "+ ia);
}
if(ia < ib){
System.out.println("最大值是: "+ ib);
}
//方式二:
int max = ia;
if (ib > max){
max = ib;
}
System.out.println("最大值是:" + max);
}
}
2.if else分支结构:
if([条件表达式]) {
[语句块1;]
} else {
[语句块2;]
}
- 判断条件表达式是否成立
=> 若成立,则执行语句块1;
=> 若不成立,则执行语句块2;
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用ifelse分支结构来模拟考试成绩查询的过程
* @author cyh
* @create 2020/12/11 9:10
*/
public class IfElseTest {
public static void main(String[] args) {
//1.提示用户输入考试成绩并使用变量记录
System.out.println("请输入您的考试成绩");
Scanner scanner =new Scanner(System.in);
int score =scanner.nextInt();
//2.使用ifelse分支结构判断考试成绩是否及格并给出对应的提示
if (score >= 60){
System.out.println("恭喜您考试通过");
} else {
System.out.println("恭喜您获得补考一份");
}
System.out.println("世界上最遥远的距离不是生与死而是你在if我在else,似乎一直相伴却又永远分离!");
}
}
案例题目
提示用户输入一个整数,使用if else分支结构判断该整数是负数还是非负 数并打印。
使用if else
分支结构判断该整数是正数、负数还是零。
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用if else分支结构判断是否为负数和非负数
* @author cyh
* @create 2020/12/11 9:18
*/
public class IfElseJudgeTest {
public static void main(String[] args) {
//1.提示用户输入一个整数并使用变量记录
System.out.println("请输入一个整数");
Scanner scanner =new Scanner(System.in);
int num =scanner.nextInt();
//2.使用ifelse分支结构判断负数和非负数并打印
if(num < 0){
System.out.println(num + "是负数");
} else{
//System.out.println(num + "是非负数");
//使用`if else`分支结构判断该整数是正数、负数还是零。
if(num > 0){
System.out.println(num + "是正数");
} else {
System.out.println(num + "是零");
}
}
}
}
3.if else if else分支结构
if([条件表达式1]) {
[语句块1;
} else if([条件表达式2]) {
[语句块2;]
} else {
[语句块n;]
}
- 件表达式1是否成立
=> 若成立,则执行语句块1;
=> 若不成立,则判断条件表达式2是否成立
=> 若成立,则执行语句块2;
=> 若不成立,则执行语句块n;
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程实现if elseif else分支结构的使用,来模拟购买火车票的过程
* @author cyh
* @create 2020/12/11 9:27
*/
public class IfElseifElseTest {
public static void main(String[] args) {
//1.提示用户输入身份信息并使用变量记录
System.out.println("请输入你的身份信息");
Scanner scanner =new Scanner(System.in);
String str =scanner.next();
//2.使用if else if else分支结构判断身份信息并给出对应的提示
if ("军人".equals(str)){
System.out.println("请免费乘车!");
} else if ("学生".equals(str)){
System.out.println("请购买半价票!");
} else {
System.out.println("请购买全价票!");
}
//3.打印一句话
System.out.println("坐上了火车去拉萨,去看那美丽的布达拉!");
}
}
1.案例题目:
根据用户输入的薪水计算个人所得税并打印出来,其中个税起征点为:
5000元,具体规则如下:
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用if elseif else分支结构来计算个人所得税
* @author cyh
* @create 2020/12/11 9:35
*/
public class IfSalaryTest {
public static void main(String[] args) {
//1.提示用户输入个人的薪水并使用变量记录
System.out.println("请输入你的薪水");
Scanner scanner =new Scanner(System.in);
int salary = scanner.nextInt();
//2.使用if lelse if else分支结构判断薪水所在的范围并计算对应的个人所得税
//方式一:
double salaryPrice =0.0;
// if (salary <= 5000){
// System.out.println("无需纳税");
// } else if (salary <= 8000){
// salaryPrice = (salary -5000) * 0.03;
// } else if (salary <= 17000){
// salaryPrice =(salary -8000) * 0.1 + (8000 - 5000) * 0.03;
// } else if (salary <= 30000){
// salaryPrice =(salary -17000) * 0.2 + (17000 - 8000) * 0.1 +(8000 - 5000) * 0.03;
// } else if (salary <= 40000){
// salaryPrice =(salary -30000) * 0.25 + (30000 - 17000) * 0.2 + (17000 - 8000) * 0.1 +(8000 - 5000) * 0.03;
// } else if (salary <= 60000){
// salaryPrice =(salary -40000) * 0.3 + (40000 - 30000) * 0.25 + (30000 - 17000) * 0.2 + (17000 - 8000) * 0.1 +(8000 - 5000) * 0.03;
// } else if (salary <= 85000){
// salaryPrice =(salary -60000) * 0.35 + (60000 -40000) * 0.3 + (40000 - 30000) * 0.25 + (30000 - 17000) * 0.2 + (17000 - 8000) * 0.1 +(8000 - 5000) * 0.03;
// } else {
// salaryPrice =(salary -85000) * 0.45 + (85000 -60000) * 0.35 + (60000 - 40000) * 0.3 + (40000 - 30000) * 0.25 + (30000 - 17000) * 0.2 + (17000 - 8000) * 0.1 +(8000 - 5000) * 0.03;
// }
//方式二://个人所得税公式:本月应纳税所得额*对应的税率-速算扣除数
if (salary <= 5000){
System.out.println("无需纳税");
} else if (salary <= 8000){
salaryPrice = (salary -5000) * 0.03;
} else if (salary <= 17000){
salaryPrice = (salary -5000) * 0.10 - 210;
} else if (salary <= 30000){
salaryPrice = (salary -5000) * 0.20 - 1410;
} else if (salary <= 40000){
salaryPrice = (salary -5000) * 0.25 - 2660;
} else if (salary <= 60000){
salaryPrice = (salary -5000) * 0.30 - 4410;
} else if (salary <= 85000){
salaryPrice = (salary -5000) * 0.35 - 7160;
} else {
salaryPrice = (salary -5000) * 0.45 -15160;
}
//3.打印最终的计算结果
System.out.println("个人的所得税为:" + salaryPrice);
}
}
2.案例题目:
出租车计费方式:由里程钱数和等候时间钱数相加得出。
里程数前3公里13元,超过3公里到15公里部分每公里2元,15公里以上部分每公里3元。
等候时间每2分半1元,不足部分不要钱。
输入公里数和等候秒数,输出车费。
16公里,等候290秒,车费 = 13 +(15-3)*2 + (16-15)*3 + 1 = 41
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用if分支结构实现出租车计费系统的实现
* @author cyh
* @create 2020/12/11 9:57
*/
public class IfTaxiTest {
public static void main(String[] args) {
//1.提示用户输入公里数和等待的秒数并使用变量记录
System.out.println("请输入公里数和等待秒数");
Scanner scanner = new Scanner(System.in);
/**
* 公里数
*/
int km =scanner.nextInt();
/**
* 等待秒数
*/
int sec =scanner.nextInt();
//2.根据公里数计算对应的里程费并使用变量记录
int kmPrice = 0;
if(km <= 3){
kmPrice = 13;
} else if (km <= 15){
kmPrice = 13 + (km - 3) * 2;
} else {
kmPrice = 13 + (15 - 3) * 2 + (km -15) * 3;
}
//3.根据等待的秒数来计算对应的等待费并使用变量记录
int secPrice = sec / 150;
//4.计算总费用并打印
int sumPrice = kmPrice + secPrice;
System.out.println("本次出租车的总费用是:"+sumPrice);
}
}
3.案例题目:
提示用户输入考试的成绩,使用if-else if-else分支结构判断所在等级并打印。
[90 ~ 100] 等级A
[80 ~ 89] 等级B
[70 ~ 79] 等级C
[60 ~ 69] 等级D
[0 ~ 59] 等级E
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用if else if else分支结构判断考试成绩所在的等级
* @author Administrator
* @create 2020/12/11 10:07
*/
public class IfScoreTest {
public static void main(String[] args) {
//1.提示用户输入考试成绩并使用变量记录
System.out.println("请输入你的考试成绩:");
Scanner scanner = new Scanner(System.in);
int score =scanner.nextInt();
//2.使用if else if else分支结构判断所在的等级并打印
if(score >100 || score <0){
System.out.println("请输入正确的成绩");
} else {
if(score >= 90 && score <= 100) {
System.out.println("等级A");
} else if(score >= 80) {
System.out.println("等级B");
} else if(score >= 70) {
System.out.println("等级C");
} else if(score >= 60) {
System.out.println("等级D");
} else {
System.out.println("等级E");
}
}
}
}
3.switch case分支结构的概念和使用
switch case分支结构
switch([变量/表达式]) {
case [字面值1]: [语句块1]; break;
case [字面值2]: [语句块2]; break;
...
default:[语句块n];break;;
}
- 计算变量/表达式的数值 => 判断是否匹配字面值1
=> 若匹配,则执行语句块1 => 执行break跳出当前结构
=> 若不匹配,则判断是否匹配字面值2
=> 若匹配,则执行语句块2 => 执行break跳出当前结构
=> 若不匹配,则执行语句块n
import java.util.Scanner;
public class SwitchScoreTest {
public static void main(String[] args) {
//1.提示用户输入考试成绩并使用变量记录 0 ~ 100
System.out.println("请输入您的考试成绩:");
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
//2.使用switch case分支结构实现考试成绩的等级判断
switch(score / 10) {
case 10: //System.out.println("等奖A"); break;
case 9: System.out.println("等奖A"); break;
case 8: System.out.println("等奖B"); break;
case 7: System.out.println("等奖C"); break;
case 6: System.out.println("等奖D"); break;
default: System.out.println("等奖E"); //break;
}
// 3.打印一句话
System.out.println("世界上最痴情的等待就是我当case你当switch,或许永远都不会选到自己!");
}
}
- switch()中支持的数据类型有:
byte
、short
、char
以及int
类型,从jdk1.5
开始支持枚举类型
,从jdk1.7开始支持String
类型。
案例题目
使用switch case分支结构模拟以下菜单效果:
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用switch case分支结构来模拟菜单的效果
* @author cyh
* @create 2020/12/12 9:21
*/
public class SwitchMenuTest {
public static void main(String[] args) {
//1.绘制字符界面
System.out.println(" 欢迎来到拉勾教育");
System.out.println("------------------------------");
System.out.print(" [1] 学员系统 ");
System.out.println(" [2] 管理员系统 ");
System.out.println(" [0] 退出系统");
System.out.println("------------------------------");
System.out.println("请选择要进入的系统:");
Scanner scanner =new Scanner(System.in);
int choose =scanner.nextInt();
//2.使用switch case分支结构来模拟用户的选择并给出提示
switch (choose){
case 1:
System.out.println("正在进入学员系统");
break;
case 2:
System.out.println("正在进入管理员系统");
break;
case 0:
System.out.println("谢谢使用,下次再见!");
break;
default:
System.out.println("请输入正确的编号");
}
}
}
二、循环结构(重中之重)
1.循环结构的概念
- 在Java程序中若希望重复执行一段代码时,就需要使用循环结构。
- 任何复杂的程序逻辑都可以通过顺序、分支、循环三种程序结构实现。
2.for循环的概念和使用
for循环结构
for([初始化表达式]; [条件表达式]; [修改初始值表达式]) {
[循环体;]
}
- 执行初始化表达式 => 判断条件表达式是否成立
=> 成立则执行循环体 => 修改初始值表达式 => 判断条件是否成立
=> 若不成立,则循环结束
package cn.cyh.flow;
/**
* 编程实现for循环的使用,模拟玩游戏的过程
* @author cyh
* @create 2020/12/12 9:41
*/
public class ForTest {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 10; i++) {
System.out.println("今晚吃鸡,大吉大利,正在进行第"+ (i + 1) +"场游戏。。。");
Thread.sleep(5000);//表示模拟睡眠5秒效果
System.out.println("本场游戏结束!\n\n\n");
}
System.out.println("该休息了,晚安!");
}
}
1.案例题目:
使用
for
循环打印1 - 100
的所有奇数,使用三种方式。
package cn.cyh.flow;
/**
* 编程使用for循环实现1 ~ 100之间所有奇数的打印
* @author cyh
* @create 2020/12/12 9:51
*/
public class ForNumTest {
public static void main(String[] args) {
// 1.使用for循环打印1 ~ 100之间的所有奇数
// 方式一:根据奇数的概念进行打印
for (int i = 1; i <= 100; i++) {
// 若当前i的数值是奇数时则打印,否则不打印 奇数就是不能被2整除的数,也就是对2取余的结果不为0
if(i % 2 != 0){
System.out.println("i = " + i);
}
}
System.out.println("---------------------------------------------------");
// 方式二:根据等差数列的概念来打印 每两个数据之间相差2
for(int i = 1; i <= 100; i += 2) {
System.out.println("i = " + i);
}
System.out.println("---------------------------------------------------");
// 方式三:根据通项公式的规则来打印 2*i-1
for(int i = 1; i <= 50; i++) {
System.out.println("i = " + (2 * i - 1));
}
}
}
2.案例题目:
使用
for
循环实现累加:1 + 2 + ... + 10000 = ?
最后打印出来。
package cn.cyh.flow;
/**
* 编程使用for循环实现1 ~ 10000之间所有整数的累加和
* @author cyh
* @create 2020/12/12 9:54
*/
public class ForSumTest {
public static void main(String[] args) {
// 2.声明一个变量负责记录累加的结果
int sum = 0;
// 1.使用for循环打印1 ~ 10000之间的所有整数
for (int i = 1; i < 10000; i++) {
sum += i;
}
System.out.println("sum = " + sum);
}
}
3.案例题目:
使用
for
循环打印三位数中所有水仙花数。
所谓“水仙花数”即一个整数满足其值等于各个数位的立方和。
如:153是一个水仙花数,因为153 = 1 ^ 3 + 5 ^ 3 + 3 ^ 3
package cn.cyh.flow;
/**
* 编程使用for循环打印三位数中的所有水仙花数
* @author cyh
* @create 2020/12/12 9:56
*/
public class ForWaterTest {
public static void main(String[] args) {
// 1.使用for循环打印所有的三位数
System.out.println("三位数中的所有水仙花数:");
for (int i = 100; i < 999; i++) {
// 2.拆分三位数中的各个数位上的数字
int hundred = i / 100; // 拆分百位数
int ten = i % 100 / 10; // 拆分十位数
int digit = i % 10; // 拆分个位数
if(hundred * hundred * hundred + ten * ten * ten + digit * digit * digit == i){
System.out.print(i +"\t");
}
}
}
}
continue关键字
continue
语句用在循环体中,用于结束本次循环而开始下一次循环。
4.案例题目:
使用
for
循环打印1 ~ 20
之间的所有整数,若遇到5
的倍数则跳过不打印。
package cn.cyh.flow;
/**
* 编程实现continue关键字的使用
* @author cyh
* @create 2020/12/12 10:03
*/
public class ForContinueTest {
public static void main(String[] args) {
//1.使用for循环打印1~20之间的所有整数
for (int i = 1; i <= 20; i++) {
if(0 == i % 5){
continue; //表示提前结束本次循环,继续下一次循环,也就是奔向了i++
}
System.out.println("i = " + i);
}
}
}
break关键字
break
用于退出当前语句块,break用在循环体中用于退出循环。for(;;)
- 这种没有循环条件的循环叫做 无限循环,俗称“死循环”
5.案例题目:
不断地提示用户输入聊天内容并输出,直到用户输入”
bye
”结束聊天。
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用for循环和break关键字来模拟聊天的过程
* @author cyh
* @create 2020/12/12 11:12
*/
public class ForBreakTest {
public static void main(String[] args) {
boolean flag =true;
Scanner scanner = new Scanner(System.in);
for (;;){
//1.提示用户输入要发送的聊天内容并使用变量记录了
System.out.println("请输入"+ (flag ?"张三":"李四") +"发送的聊天内容:");
String str =scanner.next();
//2.判断用户输入的内容是否为"bye",若是则聊天结束
if("bye".equals(str)){
System.out.println("聊天结束");
break;
}
//3.若不是则打印用户输入的聊天内容
System.out.println((flag ?"张三":"李四") + "说: " + str);
flag = !flag;
}
}
}
6.案例题目:
猜数字游戏
随机生成数字n(1-100), 等待用户输入猜测数据, 根据用户的输入比较输出
猜大了,猜小了,猜对了, 如果用户猜对了就结束游戏 。
package cn.cyh.flow;
import java.util.Random;
import java.util.Scanner;
/**
* 编程使用for循环实现猜数字游戏
* @author cyh
* @create 2020/12/12 11:38
*/
public class ForGuessTest {
public static void main(String[] args) {
//1.随机生成1~100之间的整数并使用变量记录
Random random = new Random();
int temp = random.nextInt(100) + 1;
// System.out.println("temp = " + temp);
//2.提示用户输入1~100之间猜测的整数并使用变量记录
System.out.println("请输入1 ~ 100之间猜测的整数并使用变量记录");
int cnt = 0 ;
for (;;){
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
cnt++;
//3.使用用户输入的整数与随机数之间比较大小并给出对应的提示
if(num > temp){
System.out.println("没有你想的那么大!!!");
} else if(num < temp){
System.out.println("怎么可能这么小!!!");
} else{
System.out.println("猜对了!不过没奖");
break;
}
}
if(1 == cnt){
System.out.println("我怀疑你作弊!");
} else if(cnt <=6){
System.out.println("一共" + cnt + "次,水平不错");
} else {
System.out.println("一共" + cnt + "次,需要提升!");
}
}
}
双重for循环结构
for([初始化表达式1]; [条件表达式2]; [修改初始值表达式3]) {
for([初始化表达式4]; [条件表达式5]; [修改初始值表达式6]) {
[循环体;]
}
}
双重for循环的执行流程
执行表达式1 => 判断表达式2是否成立
=> 若成立,则执行表达式4 => 判断表达式5是否成立
=> 若成立,则执行循环体 => 执行表达式6 => 表达式5是否成立
=> 若不成立,则内层循环结束 => 表达式3 => 表达式2是否成立
=> 若不成立,则外层循环结束
package cn.cyh.flow;
/**
* 编程实现双重for循环的使用
* @author cyh
* @create 2020/12/12 12:03
*/
public class ForForTest {
public static void main(String[] args) {
// 1.使用for循环打印5行字符串内容"厉害了我的哥!"
for(int i = 1; i <= 5; i++) {
System.out.println("厉害了我的哥!");
}
System.out.println("------------------------------------");
// 2.使用for循环打印5列字符串内容"厉害了我的哥!"
for(int j = 1; j <= 5; j++) {
System.out.print("厉害了我的哥!");
}
System.out.println();
System.out.println("------------------------------------");
// 3.使用for循环打印5行5列字符串内容"厉害了我的哥!"
// 外层循环主要用于控制打印的行数
for(int i = 1; i <= 5; i++) {
// 内层循环主要用于控制打印的列数
for(int j = 1; j <= 5; j++) {
System.out.print("厉害了我的哥! ");
}
System.out.println();
}
}
}
双重for循环的特点
- 外层循环用于控制打印的行数,内层循环用于控制打印的列数,外层循 环改一下,内层循环从头到尾跑一圈。
- 在以后的开发中若需要打印多行多列时,需要使用双重循环。
- 多重循环不宜嵌套太多层,否则效率很低。一般到三重循环即可,最常 见的就是双重循环。
1.案例题目:
使用双重
for
循环分别打印以下图案
package cn.cyh.flow;
/**
* 编程使用双重for循环打印星星图案
* @author cyh
* @create 2020/12/12 14:19
*/
public class ForForStarTest {
public static void main(String[] args) {
//1.打印第一个星星图案
// 外层循环主要用于控制打印的行数
for(int i = 1; i <= 5; i++) {
// 内层循环主要用于控制打印的列数
for(int j = 1; j <= 5; j++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println("---------------------------------");
//2.打印第二个星星图案
// 外层循环主要用于控制打印的行数
for(int i = 1; i <= 5; i++) {
// 内层循环主要用于控制打印的列数
for(int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
System.out.println("---------------------------------");
//3.打印第三个星星图案
// 外层循环主要用于控制打印的行数
for(int i = 1; i <= 5; i++) {
// 内层循环主要用于控制打印的列数
for(int j = 5; j >= i; j--) {
System.out.print("* ");
}
System.out.println();
}
System.out.println("---------------------------------");
//4.打印第四个星星图案
// 外层循环主要用于控制打印的行数
for(int i = 1; i <= 5; i++) {
// 内层循环主要用于控制打印的列数
for(int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i * 2 -1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
2.案例题目:
使用双重
for
循环打印九九乘法表。
package cn.cyh.flow;
/**
* 使用双重for循环打印九九乘法表
* @author cyh
* @create 2020/12/12 14:31
*/
public class ForForTableTest {
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) +"\t");
}
System.out.println();
}
}
}
break关键字跳出多层循环
- 在嵌套的循环结构中,
break
用于退出所在循环体。 - 如果要退出外层循环体,需要使用标号的方式。
for (...) {
for(...) {
break;
} }
} }
System.out.println("---------------------")
//大跳:跳出外层for循环
[自定义]: for (...) {
for(...) {
break [自定义];
} }
} }
3.案例题目:
使用双重
for
循环打印2 ~ 100之间的所有素数。
当一个数只能被1和它本身整除时,这个数就叫做素数或质数。
package cn.cyh.flow;
/**
* 编程使用双重for循环打印2~100之间的所有素数
* @author Administrator
* @create 2020/12/12 15:00
*/
public class ForForPrimeTest {
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
boolean flag = true;
for (int j = 2; j < i; j++) {
if(0 == i % j){
flag = false;
break;
}
}
if(flag){
System.out.println(i);
}
}
}
}
2.while循环的概念和使用
while循环结构
while(条件表达式) {
循环体;
}
- 判断条件表达式是否成立
=> 若成立,则执行循环体 => 判断条件表达式是否成立
=> 若不成立,则循环结束
package cn.cyh.flow;
/**
* 编程实现while循环的使用
* @author cyh
* @create 2020/12/12 15:30
*/
public class WhileTest {
public static void main(String[] args) {
// 1.使用for循环打印1 ~ 10之间的所有整数
// 在()或{}中声明的变量叫做块变量,作用范围是从声明开始一直到语句块结束
for(int i = 1; i <= 10; i++) {
System.out.println("i = " + i);
}
System.out.println("-----------------------------");
// 2.使用while循环打印1 ~ 10之间的所有整数
int i = 1;
while(i <= 10) {
System.out.println("i = " + i);
i++;
}
}
}
案例题目
使用
while
循环计算调和数列的和并打印,即: 1/1 + 1/2 + … + 1/n
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用while循环实现调和数列的累加和并打印
* @author Administrator
* @create 2020/12/12 15:31
*/
public class WhileSumTest {
public static void main(String[] args) {
// 1.提示用户输入一个整数并使用变量记录
System.out.println("请输入一个整数:");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
// 2.使用while循环计算调和数列的和并使用变量记录
double sum = 0.0;
int i = 1;
while(i <= num) {
sum += 1.0/i;
i++;
}
// 3.打印最终的计算结果
System.out.println("最终的计算结果是:" + sum);
}
}
while循环和for循环比较
while
循环和for
循环完全可以互换,当然推荐使用for循环。while
循环更适合于明确循环条件但不明确循环次数的场合中。for
循环更适合于明确循环次数或范围的场合中。while(true)
等价于for(;;)
都表示无限循环。
案例题目
提示用户输入一个任意位数的正整数然后反向输出。
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用while循环实现任意正整数的反向输出
* @author cyh
* @version 2020/12/13 10:51
*/
public class WhileReverseTest {
public static void main(String[] args) {
//1.提示用户输入一个正整数并使用变量记录
System.out.println("请输入一个正整数:");
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
//2.使用while循环进行拆分并打印
// while (num > 0){
// System.out.print(num % 10);
// num /= 10;
// }
//2.使用while循环进行拆分并打印
int res = 0;
int temp = num;
while (temp > 0){
res = res * 10 + temp %10;
temp /= 10;
}
System.out.println("原来的数:" + num);
System.out.println("逆序的数:" + res);
}
}
do while循环(熟悉)
do {
循环体;
} while(条件表达式);
- 执行循环体 => 判断条件表达式是否成立
=> 若成立,则执行循环体 => 判断条件表达式是否成立
=> 若不成立,则循环结束 - do-while循环主要用于至少执行一次循环体的场合中。
package cn.cyh.flow;
/**
* 编程实现do while循环的使用
* @author cyh
* @version 2020/12/13 11:03
*/
public class DoWhileTest {
public static void main(String[] args) {
//使用do while 循环打印 1 ~ 10 之间的整数
int i = 1;
do {
System.out.println("i = " + i);
i++;
}while (i <= 10);
}
}
案例题目
使用do while循环来模拟学习任务是否合格的检查, 如果合格则停止,
否则就重新完成学习任务。
package cn.cyh.flow;
import java.util.Scanner;
/**
* 编程使用do while循环来模拟学习效果的检查
* @author cyh
* @version 2020/12/13 11:08
*/
public class DoWhileCheckTest {
public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
String msg;
do {
System.out.println("学习中...");
Thread.sleep(5000);
System.out.println("是否合格?(Y/N)");
msg = scanner.next();
}while(!"y".equalsIgnoreCase(msg));
System.out.println("恭喜你,任务合格");
}
}
三、知识总结
- 分支结构
分支结构、if else
分支结构、if else if else
分支结构、switch case
分支结构 - 循环结构
for
循环、break
continue
关键字、双重for
循环、while
循环、do while
循环