流程控制语句
1.了解流程控制语句的分类
1.顺序结构
2.分支结构/选择结构
3.循环结构
2. 了解 顺序结构
/*
顺序结构:是代码执行的一般顺序
执行流程:
从上到下,从左到右
*/
public class Demo01 {
public static void main(String[] args) {
//从上到下
System.out.println("开始");
System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("D");
System.out.println("E");
System.out.println("结束");
//从左到右
System.out.println(1+99+"年黑马");
System.out.println(1+2+3+4+5+"HelloWorld"+6+7+8+9);
}
}
3.分支结构/选择结构
3.1 分支结构的分类
1.if语句
2.switch语句
3.2 if语句
3.2.1if语句格式1:
/*
if语句格式1:
if(关系表达式/条件表达式){
语句体
}
执行流程:
1.判断关系表达式的值
2.如果为true执行语句体
3.如果为false 不执行语句体,执行if语句外的其他语句
*/
public class Demo02 {
public static void main(String[] args) {
System.out.println("开始");
int a = 10;
a = 11;
if (a>10){
System.out.println("a大于10");
}
System.out.println("结束");
}
}
3.2.2 if语句格式2
/*
if语句格式2:
if(关系表达式/条件表达式){
语句体1;
}else{
语句体2;
}
执行流程:
1.判断关系表达式的值
2.如果值为true 执行语句体1;
3.如果值为false 执行语句体2;
*/
public class Demo03 {
public static void main(String[] args) {
System.out.println("开始");
int a = 10;
a = 11;
if (a > 10) {
System.out.println("a大于10");
} else {
System.out.println("a不大于10");
}
System.out.println("结束");
}
}
3.2.3 if语句格式3
/*
if语句格式3:
if(条件表达式1){
语句体1
}else if(条件表达式2){
语句体2
}... ...
else{
语句体 n+1
}
执行流程:
1.判断条件表达式1的值
2.如果条件表达式1值为 true 执行语句体1
3.如果条件表达式1值为 false 执行条件表达式2,判断条件表达式2的值
4.如果条件表达式2的值为true 执行语句体2
5.如果条件表达式2的值为false,继续判断下一个条件表达式
... ...
6.如果所有的条件表达式的值都为false 就执行最后一个else后面的语句体n+1
*/
public class Demo04 {
public static void main(String[] args) {
int a = 10;
a = 9;
a = 20;
if (a>10){
System.out.println("a大于10");
}else if (a<10){
System.out.println("a小于10");
}else {
System.out.println("a等于10");
}
}
}
3.3 switch语句
3.3.1 switch入门
import java.util.Scanner;
/*
switch语句:
格式:
switch(值){
case 值:语句体1;break
case 值:语句体2;break
case 值:语句体3;break
case 值:语句体4;break
... ...
default 语句体n+1;break
}
执行流程:
1.获取switch语句后面小括号中的值,
2.拿个这个值,到switch中的case语句后面值相匹配,如果匹配的上,就执行case语句后面的语句体
3.switch语句后面小括号中的值和所有的case语句后面的值,都不匹配,执行default语句
*/
public class Demo05 {
public static void main(String[] args) {
//键盘录入一个星期数,输出对应星期几
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个星期数:");
int week = sc.nextInt();
//使用switch语句匹配
switch (week){
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("您输入的星期数有误!");
}
}
}
3.3.2 case语句具有穿透性
import java.util.Scanner;
/*
键盘录入一个月份输出对应季节
月份对应的季节:
3 4 5 春季
6 7 8 夏季
9 10 11 秋季
12 1 3 冬季
case语句的特点:
1.case遇到break才停止执行,没有break直接向下顺序执行,直到遇到break才停止,如果没有break,直接执行到程序的结束
这个现象又叫做case语句的穿透性
*/
public class Demo06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个月份:");
int month = sc.nextInt();
//使用switch语句输出对应的季节
switch (month){
case 12:
case 1:
case 2: System.out.println("冬季");break;
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;
default: System.out.println("您输入的月份有误!");break;
}
}
}
4. 循环换结构
4.1 循环分类
1.for循环
2.while循环
3.dp-while循环
4.2 for循环
/*
for循环
格式:
for(初始化语句;条件判断语句;条件控制语句){
循环体语句;
}
执行流程:
1.初始化语句
2.条件判断语句==>true ==> 循环体语句 ==> 条件控制语句 ==> 2.条件判断语句
3.直到条件判断语句结果为false 结束整个for循环
*/
public class Demo01 {
public static void main(String[] args) {
//打印5行HelloWorld
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("--------------------");
for (int i = 1;
i <= 5;
i++) {
System.out.println("HelloWorld");
}
}
}
5.经典习题
5.1 使用for循环打印1–5 使用for循环打印5–1
/*
使用for循环打印1--5
使用for循环打印5--1
*/
public class Demo02 {
public static void main(String[] args) {
//使用for循环打印1--5
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
System.out.println("----------");
//使用for循环打印5--1
for (int i = 5; i >= 1; i--) {
System.out.println(i);
}
}
}
5.2 1–100之间的数据和
/*
使用for循环 求1--100之间的数据和
分析:
1.定义变量用来存储 最终数据的和
2.获取1--100之间的整数,累加
使用for循环
*/
public class Demo03 {
public static void main(String[] args) {
//定义变量
int sum = 0;
//使用循环获取1--100.之间的数字
for (int i = 1; i <= 100; i++) {
// System.out.println(i);
sum = sum + i;
}
System.out.println("1--100之间的数据和:"+sum);
}
}
5.3 1–100之间所有奇数和
/*
使用循环求1--100之间所有偶数和
使用循环求1--100之间所有奇数和
*/
public class Demo04 {
public static void main(String[] args) {
//使用循环求1--100之间所有偶数和
//定义变量存储最终的和
int sum1 = 0;
/*//使用循环获取1--100之间所有数字
for (int i = 1; i <= 100; i++) {
//判断是偶数才累加
if (i%2==0){
sum1 = sum1+i;
}
}*/
for (int i = 2; i <= 100; i+=2) {
sum1+=i;
}
//打印偶数和
System.out.println("1--100之间所有偶数和:"+sum1);
//使用循环求1--100之间所有奇数和
//定义变量存储最终的和
int sum2 = 0;
/*//使用循环获取1--100之间所有数字
for (int i = 1; i <= 100; i++) {
//判断是奇数才累加
if (i%2!=0){
sum2+=i;
}
}*/
for (int i = 1; i <= 100; i+=2) {
sum2+=i;
}
//打印奇数和
System.out.println("1--100之间所有奇数和:"+sum2);
}
}
5.4 while循环
/*
while 循环
格式:
初始化语句
while(条件判断语句){
循环体语句;
条件控制语句;
}
执行流程:
1.执行初始化语句
2.执行条件判断语句 ==> true ==> 循环体语句 ==> 条件控制语句 ==> 2.执行条件判断语句
3.知道条件判断语句结果为false结束整个while循环
*/
public class Demo05 {
public static void main(String[] args) {
//打印1---5
for (int i = 1; i <= 5; i++) { //正向遍历 数字.fori
System.out.println(i);
}
System.out.println("----------------");
int j = 1; //初始化语句
while (j<=5){ //条件判断语句
System.out.println(j);//循环体语句;
j++; //条件控制语句;
}
System.out.println("-----------------");
//打印5---1
for (int i = 5; i > 0; i--) { //逆序遍历 数字.forr
System.out.println(i);
}
System.out.println("------------");
int k = 5;
while (k>=1){
System.out.println(k);
k--;
}
}
}
5.5 while循环的练习
/*
需求:世界最高山峰是珠穆朗玛峰(8844.43米=8844430毫米),假如我有一张足够大的纸,它的厚度是0.1毫米。
请问,我折叠多少次,可以折成珠穆朗玛峰的高度?
分析:
1.定义变量的时候要统一数据类型 都是用毫米
2.未知的折叠次数,但是我们折叠什么时候不折叠,纸张的厚度大于等于珠峰的好高度的时候 停止折叠
3.纸张折叠的算法
0.1 0.2 0.4 0.8 1.6 3.2 6.4 12.8 .......
*/
public class Demo06 {
public static void main(String[] args) {
//定义计数器
int count = 0;
//定义变量
double paper = 0.1;
double zf = 8844430;
while (paper<zf){
paper *= 2;
//计数器 纸张没折叠一次,计数器就要加一次
count++;
}
System.out.println("折叠"+count+"次,可以折成珠穆朗玛峰的高度");
}
}
5.6 do-while循环
/*
do-while循环:
格式:
初始化语句
do{
循环体语句
条件控制语句
}while(条件判断语句);
执行流程:
1.初始化语句
2.循环体语句
3.条件控制语句
4.条件判断语句 ==> true ==> 2.循环体语句 ==> 3.条件控制语句 ==> 4.条件判断语句
5.知道条件判断语句结果为false 结束整个do-while循环
*/
public class Demo07 {
public static void main(String[] args) {
System.out.println("---------for------------");
//打印1---5
for (int i = 1; i <= 5; i++) { //正向遍历 数字.fori
System.out.println(i);
}
System.out.println("--------while--------");
int j = 1; //初始化语句
while (j<=5){ //条件判断语句
System.out.println(j);//循环体语句;
j++; //条件控制语句;
}
System.out.println("---------do-while---------");
int m = 1;
do {
System.out.println(m);
m++;
}while (m<=5);
System.out.println("-------for----------");
//打印5---1
for (int i = 5; i > 0; i--) { //逆序遍历 数字.forr
System.out.println(i);
}
System.out.println("------while------");
int k = 5;
while (k>=1){
System.out.println(k);
k--;
}
System.out.println("--------do-while----------");
int n = 5;
do {
System.out.println(n);
n--;
}while (n>=1);
}
}
5.7 三种循环的区别
/*
三种循环的区别:
1.for循环和while循环 先执行条件判断语句,do-while循环 先执行循环体语句
2.for循环的初始化语句在循环的声明上,循环结束就不能使用
while循环和do-while循环 初始化语句在循环外面,循环结束后,还可以继续使用
*/
public class Demo08 {
public static void main(String[] args) {
System.out.println("---------for------------");
//打印1---5
for (int i = 1; i <= 5; i++) { //正向遍历 数字.fori
System.out.println(i);
}
System.out.println("--------while--------");
int j = 1; //初始化语句
while (j <= 5) { //条件判断语句
System.out.println(j);//循环体语句;
j++; //条件控制语句;
}
System.out.println("j = " + j);
System.out.println("---------do-while---------");
int m = 1;
do {
System.out.println(m);
m++;
} while (m <= 5);
System.out.println("m = " + m);
}
}
5.8 考试奖励
import java.util.Scanner;
/*
需求:小明快要期末考试了,小明爸爸对他说,会根据他不同的考试成绩,送他不同的礼物,
假如你可以控制小明的得分,请用程序实现小明到底该获得什么样的礼物,并在控制台输出。
*/
public class Demo01 {
public static void main(String[] args) {
//键盘录入
Scanner sc = new Scanner(System.in);
System.out.println("请输入小明的分数:");
int score = sc.nextInt();
//根据小明的成绩给出不同的奖励
if (score > 100 || score < 0) {
System.out.println("无效的成绩!");
} else if (score >= 95 && score <= 100) {
System.out.println("奖励山地车一辆!");
} else if (score >= 90 && score <= 94) {
System.out.println("游乐场玩一圈");
} else if (score >= 80 && score <= 89) {
System.out.println("变形金刚一个");
} else if (score <= 80) {
System.out.println("胖揍一顿!");
}
}
}
5.9 水仙花
/*
在控制台输出所有的“水仙花数”
什么是“水仙花数”?
1.水仙花数是一个三位数
111 222 333 370 371 520 999
2.水仙花数的个位、十位、百位的数字立方和等于原数
153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
分析:
1.获取所有的三位数
for循环
2.求出每个三位数各个位上的数字
3.求各个位上数字的立方和
4.比较 如果立方和 和数字本身相等 就是水仙花数
*/
public class Demo02 {
public static void main(String[] args) {
//获取所有的三位数
for (int i = 100; i <= 999; i++) {
//System.out.println(i);
//求出每个三位数各个位上的数字
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100 % 10;
//求各个位上数字的立方和
int sum = ge*ge*ge + shi*shi*shi + bai*bai*bai;
//比较 如果立方和 和数字本身相等 就是水仙花数
if (sum == i){
//输出水仙花数
System.out.println(i);
}
}
}
}
6.死循环和流程跳转控制语句
6.1 死循环
/*
死循环:
格式:
for(;;){}
while(true){} 推荐使用
do{}while(true);
*/
public class Demo01 {
public static void main(String[] args) {
/*for(;;){
System.out.println("不要停.....");
}*/
/*while(true){
System.out.println("停不下来了......");
}*/
do{
System.out.println("不行啦.......");
}while(true);
}
}
6.2 流程跳转控制语句
/*
流程跳转控制语句:
break; 停止当前循环
continue; 跳过本次循环继续下一次
*/
public class Demo02 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (i == 2){
break;
}
System.out.println(i);// 0 1
}
System.out.println("----------------");
//continue; 跳过本次循环继续下一次
for (int i = 0; i < 5; i++) {
if (i == 2){
continue;
}
System.out.println(i); // 0 1 3 4
}
}
}
7.嵌套的for循环
7.1 入门
/*
@@@@@
@@@@@
@@@@@
@@@@@
每次只能打印一个@;
System.out.print(); //打印数据没有换行的效果
System.out.println();//打印数据具有换行的效果
*/
public class Demo03嵌套for循环 {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) { //外层for循环控制行数
for (int j = 1; j <= 5; j++) {//内层for循环打印的是列数,也就是说一行打印激烈
System.out.print("@");
}
System.out.println();
}
}
}
7.2 九九乘法表
public class Demo04九九乘法表 {
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+"\t");
}
System.out.println();
}
}
}
8. 随机数
8.1 随机数入门
import java.util.Random;
/*
Random的使用步骤:
1.导包
import java.util.Random
2.创建随机数的对象
Random r = new Random();
3.生成随机数
int num = r.nextInt(数字);//java规定数字必须大于0
随机获取生成了0到这个数字减一之间的整数
*/
public class Demo01 {
public static void main(String[] args) {
//创建随机数的对象
Random r = new Random();
//生成随机数
for (int i = 0; i < 100; i++) {
int num = r.nextInt(10);//0到9之间的随机数
//打印结果
System.out.println(num);
}
}
}
8.2 猜数字小游戏
import java.util.Random;
import java.util.Scanner;
/*
需求:程序自动生成一个1-100之间的数字,使用程序实现猜出这个数字是多少?
当猜错的时候根据不同情况给出相应的提示
如果猜的数字比真实数字大,提示你猜的数据大了
如果猜的数字比真实数字小,提示你猜的数据小了
如果猜的数字与真实数字相等,提示恭喜你猜中了
分析:
1.生成随机数 1--100之间的随机数
导包 创建对象 生成随机数
2.猜数字
键盘录入
死循环猜数字
猜对了就停止,如果猜的数字与真实数字相等,提示恭喜你猜中了
猜不对给出提示:
如果猜的数字比真实数字大,提示你猜的数据大了
如果猜的数字比真实数字小,提示你猜的数据小了
*/
public class Demo02 {
public static void main(String[] args) {
//生成随机数 1--100之间的随机数
Random r = new Random();
int num = r.nextInt(100) + 1;//1--100
//System.out.println(num);
//猜数字
Scanner sc = new Scanner(System.in);
while (true) { //选中代码 Ctrl+Alt +T 选while就是while死循环
System.out.println("请输入你要猜的数字:");
int guessNum = sc.nextInt();
if (guessNum > num) {
System.out.println("你猜的数据大了");
} else if (guessNum < num) {
System.out.println("你猜的数据小了");
} else if (guessNum == num) {
System.out.println("恭喜你猜中了");
break;
}
}
}
}