目录
流程控制
一个Java程序执行,意味着多条语句的执行,执行结果与执行顺序有关。
流程控制:控制多条语句的执行顺序(三种方式:顺序结构、选择结构、循环结构 )
顺序结构
按照书写顺序从上到下、从左到右依次执行的执行顺序
选择结构
代码有选择的执行,Java中有2种实现形式:if 语句和 switch 语句
if 语句
/*
if实现选择结构
1.单分支的选择结构:如果...就...
if(判断条件){//判断条件结果只能是true 和 false
//语句体
}
2.双分支的选择结构:如果...就...否则...
if(判断条件){
//语句体
} else {
//语句体
}
3.多分支(各个分支,平行、互斥、只执行一个语句体)
if(判断条件1){
//语句体
} else if(判断条件2){
//语句体
} else {
//语句体
}
*/
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
//单分支:当用户输入正数就输出,否则不输出
if(input > 0){
System.out.println("正数"+input);
}
//---------------------------------------
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
//双分支:当用户输入正数就输出,否则提示请输入整数
if(input > 0){
System.out.println("正数"+input);
} else {
System.out.println("请重新输入正数");
}
//---------------------------------------
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
//双分支:当用户输入正数就输出,否则提示请输入整数
if(input > 0){
System.out.println("正数"+input);
} else if(input == 0){
System.out.println("输入为0");
} else {
System.out.println("输入为负数"+input);
}
规范:不管单条语句还是多条语句,都放在{ }中
练习:
1、两数取大
int a = 3;
int b = 10;
int max;
if(a > b){
max = a;
} else {
max = b;
}
System.out.println(max);
//三目运算符与双分支结构,语义几乎完全相同,可以相互替代。但是三目运算符需要有个变量接收三目运算的值
max = a > b ? a : b;
System.out.println(max);
//a > b ? System.out.println(a) : System.out.println(b);//错误,没有变量接收三目运算的值
2、键盘录入x的值,并输出y的值。
计算规则:
x >= 3 , y = 2x + 1;
-1 <= x < 3, y = 2x;
x <= -1, y = 2x -1;
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); //输入-1
int y;
if(x >= 3){
y = 2 * x + 1;
} else if(x >= -1 && x < 3){
y = 2 * x;
} else {
y = 2 * x -1;
}
System.out.println("x=" + x + " y=" + y);//x=-1 y=-2
3、键盘录入月份的值,输出对应的季节
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();//输入1
if (x == 12 || x == 1 || x == 2){
System.out.println("冬季");//输出
}else if (x >= 3 && x <= 5){
System.out.println("春季");
}else if (x >= 6 && x <= 8){
System.out.println("夏季");
}else if (x >= 9 && x <= 11){
System.out.println("秋季");
}else {
System.out.println("输入错误");
}
4、三数比大
int a = 10;
int b = 20;
int c = 30;
//嵌套
if (a > b) {
if (a > c) {
System.out.println("max=" + a);
} else {
System.out.println("max=" + c);
}
} else {
if (b > c) {
System.out.println("max=" + b);
} else {
System.out.println("max=" + c);//max=30
}
}
//非嵌套
int tmp;
if (a > b) {
tmp = a;
} else {
tmp = b;//tmp = 20
}
if (tmp > c) {
System.out.println(tmp);
} else {
System.out.println(c);30
}
switch语句
switch语句格式:
switch(表达式){ //表达式表示选择条件 ,表达式的值依次匹配case分支
case 值1: //代表一个分支
语句体1; //满足条件的分支执行的语句体
break; //结束switch语句
case 值2:
语句体2;
break;
...
default: //其他分支都不匹配,则执行此分支;类似 if - else 中最后一个else
语句体n+1;
break;
}
注意:
1. switch表达式结果类型:byte、short、int、char、String
2. case后的常量取值?case后的值不能相同,必须是常量取值,必须和表达式的类型相同
3. break可以省略吗?不再是多分支,将匹配到的case分支及之后所有分支全部输出,直到遇到break
4. default可以省略吗?语法上可以,但是匹配不到case分支时什么也不做
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();//输入:1
switch (i) {
case 0:
System.out.println("--0--");
break;
case 1:
System.out.println("--1--");//输出:--1--
break;
default:
System.out.println("输入了其他值");
break;
}
//省略break
switch (i) {
case 0:
System.out.println("--0--");
case 1:
System.out.println("--1--");//输出:--1--
default:
System.out.println("输入了其他值");//输出:输入了其他值
}
练习:
1、模拟做单项选择题,根据你的选择给出对应的答案(表达式是字符的情况)
2、键盘录入字符串,根据给定的字符串,来输出你选择的字符串是什么?(表达式是字符串的情况)
3、switch语句键盘录入月份的值,输出对应的季节
答案:
1、
System.out.println("今天周几?");
System.out.println("A:周一");
System.out.println("B:周二");
System.out.println("C:周三");
System.out.println("D:周四");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
char c = s.charAt(0);//取第一个字符
switch (c) {
case 'A':
System.out.println("今天周一");
break;
case 'B':
System.out.println("今天周二");
break;
case 'C':
System.out.println("今天周三");
break;
case 'D':
System.out.println("今天周四");
break;
default:
break;
}
/*
今天周几?
A:周一
B:周二
C:周三
D:周四
C //输入
今天周三
*/
2、
System.out.println("今天周几?");
System.out.println("A:周一");
System.out.println("B:周二");
System.out.println("C:周三");
System.out.println("D:周四");
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
switch (s) {
case "A":
System.out.println("今天周一");
break;
case "B":
System.out.println("今天周二");
break;
case "C":
System.out.println("今天周三");
break;
case "D":
System.out.println("今天周四");
break;
default:
break;
}
3、
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
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;
}
/*
7
夏季
*/
选择结构的两种实现有什么应用场景?
if 的使用场景
针对结果是boolean类型的判断if(true)
分支对应多个表达式的多个取值
switch的使用场景
针对结果是固定类型的判断switch(byte,short,int,char,String)
分支对应多个表达式的某一种取值
循环结构
(重复执行一部分代码)
for循环
while循环
do...while循环
四部分组成(所有循环结构):
初始化语句
条件判断语句
循环体语句
循环控制语句
for循环控制
for(初始化语句; 条件判断语句; 条件控制语句){
循环体语句;
}补充写法(针对数组遍历):
for(数据类型 变量名 : 数组名){
循环体语句;
}
例子:hello 输出10次
//1.设定一个计数值,该计数值初始值为0(初始化语句)
//2.开始重复执行 hello(循环体语句) 每运行一次计数值 + 1(循环控制语句:改变循环变量的值)
//3.计数值 和 10 比较一下,当计数值 > 10 结束执行(条件判断语句)
for(int i = 0; i < 10; i++){
System.out.println("hello" + i);
}
//i 的作用域只在for循环中,出了for循环访问不到i
System.out.println(i);//错误 Cannot resolve symbol 'i'
数组遍历写法
//普通遍历
int[] array = {1, 2, 3, 4};
for (int i = 0; i < 4; i++) {
System.out.println(array[i]);
}
//增强形式for循环
for (int element : array) {
System.out.println(element);
}
练习:
1、请在控制台输出数据1-10
2、请在控制台输出数据10-1
3、求出1-10之间数据之和
4、求出1-100之间偶数和
5、求5的阶乘
6、在控制台输出所有的“水仙花数”
7、统计“水仙花数”共有多少个
答案:
1、
for(int i = 1; i <= 10; i++){
System.out.println(i);
}
2、
for(int i = 10; i >= 1; i--){
System.out.print(i + " ");
}
3、
// ((((0+1)+2)+3)+4)+...
int acc = 0;
for(int i = 0; i <= 10; i++){
acc = acc + i;
}
System.out.print(acc);
4、
int acc = 0;
for(int i = 0; i <= 10; i++){
if(i % 2 == 0){
acc += i;
}
}
System.out.print(acc);
5、
int multi = 1;
for(int i = 5; i >= 1; i--){
multi *= i;
}
System.out.print(multi);
6、
//水仙花数:各位上的数字的立方和等于该数字本身
//选择[100,999]
for (int i = 100; i < 1000; i++) {
//拆解得到个位上的数字
int m = i % 10;
//拆解得到十位上的数字
int n = i / 10 % 10;
//拆解得到百位上的数字
int b = i / 100;
if (m * m * m + n * n * n + b * b * b == i) {
System.out.println(i);
}
}
7、
int count = 0;
for (int i = 100; i < 1000; i++) {
int m = i % 10;
int n = i / 10 % 10;
int b = i / 100;
if (m * m * m + n * n * n + b * b * b == i) {
count++;
}
}
System.out.println(count);
while循环控制
初始化语句;
while(条件判断语句){
循环体语句(包含条件控制语句)
}
for 与 while 联系:任何场景下都可以作等价替换
for 与 while 区别:for 循环次数明确;while 循环次数不明确
例子:
我国最高山峰珠穆朗玛峰:8848米,现有一纸张厚度为0.01米。请问纸张折叠多少次厚度不低于珠穆朗玛峰的高度?
double height = 0.01;
int count = 0;
while (height < 8848) {
height *= 2;
count++;
}
System.out.println(count+" "+height);//20 10485.76
do...while循环控制
初始化语句;
do{
循环体语句(包含条件控制语句)
}while(条件判断);
不管是否满足条件都会执行一次循环体语句,之后和for、while执行流程一样
int i = 10;
do {
System.out.print(i + " ");
i++;
} while (i > 11);
//输出:10
i = 10;
while (i > 11) {
System.out.println(i + " ");
i++;
}
//没执行循环体
三种循环结构综合比较
-
for循环和while循环等价
-
do...while 保证循环体至少执行一次
-
用哪个都可以
注意: 死循环(无限循环) for(;;) 、while(true)
练习:
1、请输出一个4行5列的星星图案
2、在控制台输出九九乘法表
答案:
1、
//重复输出5行
for (int i = 0; i < 4; i++) {
//重复输出每一行的*
for (int j = 0; j < 5; j++) {
System.out.print("*");
}
//换行
System.out.println();
}
2、
for (int i = 1; i <= 9; i++) {
//控制每一行输出的表达式
for (int j = 1; j <= i; j++) {
System.out.print(j + "x" + i + "=" + i * j + " ");
}
System.out.println();
}
/*
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
*/
跳转控制语句
break;
continue;
return;
break 的使用场景:
跳出(终止)单层循环(如果有多层循环,只跳出内层)
结束switch语句,通常配合if 使用
离开使用场景的存在是没有意义的
break 的作用:
跳出(终止)单层循环(如果有多层循环,只跳出内层)
结束switch语句
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
if (i == 5) {
break;
}
}
//输出:1 2 3 4 5
//终止外层循环
outer://随意命名标签
for (int i = 0; i < 4; i++) {
//重复输出每一行的*
for (int j = 0; j < 5; j++) {
System.out.println(i+" "+j);
if(j==2)
break outer;
}
}
/*
0 0
0 1
0 2
*/
continue 的使用场景:
在循环语句中
离开使用场景的存在是没有意义的
continue 的作用:
退出循环的一次迭代过程,继续下次循环
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;//终止本次循环
}
System.out.print(i + " ");
}
//输出:1 2 3 4 6 7 8 9 10
return 不是为了跳出循环体,也可以用来跳出循环体,不管有多少层嵌套,全部终止
常用:结束一个方法(函数),也就是退出一个方法