1. 分支结构
1.1 if 结构
1 if (/* 判断语句 true false */) {
2 // 语句体
3 }
4 /*
5 执行流程:
6 当程序运行到if结构时,首先判断if之后的小括号里面的判断语句是否为true,如果为true,执行语句体,如果为false,执行大括号以外的内容
7 */
1.2 if - else 结构
1 if (/* 判断语句 true false */) {
2 // true语句体
3 } else {
4 // false 语句体
5 }
执行流程:
当前程序运行到if - else 结构时,首先判断if之后小括号里面的内容结果是什么?如果为true执行true语句体,如果为false,执行false语句体。世界上最遥远的距离,是我再 if 里 ,你在 else 里。
1.3 if - else if 结构
1 if (/* 条件匹配 1 */) {
2 // 处理方式1
3 } else if (/* 条件匹配2 */) {
4 // 处理方式2
5 } else if (/* 条件匹配 3 */) {
6 // 处理方式3
7 } else {
8 // 没有任何匹配情况下的最终处理方式
9 }
执行流程:
当前程序运行到if - else if 结构时,匹配if之后的条件,找到对应匹配条件之后,执行对应的处理方式,如果没有任何一个条件匹配,执行else里面的处理方式。
1.4 switch - case结构
1 switch (/* 条件匹配 */) {
2 case /* 常量1 */:
3 /* 处理方式1 */
4 break;
5 case /* 常量2 */:
6 /* 处理方式2 */
7 break;
8 case /* 常量3 */:
9 /* 处理方式3 */
10 break;
11 default:
12 /* 最终处理方式 */
13 break;
14 }
执行流程:
当程序运行到switch - case 结构,使用switch 之后小括号内的变量,匹配case之后的常量,找到对应的数据,按照对应的处理方式进行,如果没有任何一个条件匹配,执行default里面的处理方式。
注意事项:
- case 选择不能出现重复内容
- break可以省略,代码会继续执行到下一个break或者switch - case 大括号
- default可以省略,如果不需要可以不写default结构
2. 循环结构
2.1 为什么使用循环!!!
在代码中,出现了大量的重复的功能,使用CV大法可以解决,但是会导致以下一些问题:
- 代码过于臃肿!!!
- 代码阅读性极差!!!
- 代码的维护性极差!!!
2.2 while 循环
1 while (/* 循环条件判断 */) {
2 // 循环体
3 // (循环条件变更)
4 }
执行流程:
代码执行到while循环结构时,首先判断while之后循环条件是否为true,如果为true执行循环体(带有循环条件变更),直到while之后的条件为false,终止循环!!!
Tips:快捷键:终止循环Ctrl + C
2.3 do - while 循环
1 do {
2 // 循环体
3 // 循环条件变更
4 } while (/* 循环条件 */)
执行流程:
程序运行到do-while循环结构时,直接执行循环体(循环条件变更),再来判断 while之后的循环条件,如果循环条件为true,继续循环,直到条件为false,终止循环!!!
2.4 while 和 do while
while循环执行每一次循环体,都是在循环条件之下执行的,而do - while第一次执行循环体,是没有任何的约束的。
所以,如果在开发中可以使用while也可以使用do while,这里建议尽量使用while循环。
已知,可控
2.5 for 循环
1 for (/*循环条件初始化*/;/*循环条件判断*/;/*循环条件变更*/) {
2 // 循环体
3 }
for循环同样需要变量初始化,判断表达式,计数器自增,循环体,在固定次数的情况下for循环比while循环更加简洁。不能固定次数的时候必须使用while或者do-while。
重点for循环的执行顺序,中括号内标注序号,其中1只执行一次,注意计数器自增是每次循环执行一轮的最后才执行的,注意:一轮,最后。
2.6 Scanner的使用
1 代码中需要用户修改数据,从而满足其他需求,但是我们不能要求用户修改代码,重新编译,重新执行!!!
2 需要在代码中提供一个让用户输入数据的方式:
3
4 1. 点技能点!!!【导包】
5 在class之前
6 import java.util.Scanner;
7
8 2. 创建一个Scanner的"变量"
9 Scanner sc = new Scanner(System.in);
10
11 3. 使用Scanner扫描器中的一些方法,从键盘上获取用户输入的数据
12 获取int类型数据 int num = sc.nextInt();
13 获取float类型数据 float num = sc.nextFloat();
14 获取double类型数据 double num = sc.nextDouble();
15 获取char类型数据 char ch = sc.nextLine().charAt(0);
区别:
1、while循环是先判断后执行
2、do - while循环是先执行一遍再判断
3、for循环同样需要变量初始化,判断表达式,计数器自增,循环体,在固定次数的情况下for循环比while循环更加简洁。不能固定次数的时候必须使用while或者do-while。
例题
1、输入任意数字,得出顺序相反的数字
1 import java.util.Scanner;
2 public class Test13 {
3 public static void main(String[] args) {
4 //输入任意数字,得出顺序相反的数字
5 Scanner input = new Scanner(System.in);
6 System.out.print("请输入数字:");
7 int num = input.nextInt();
8 while(num > 0) {
9 System.out.print(num%10);
10 num /= 10;
11 }
12 }
13 }
2、用for循环输出100次好好学习
1 public class Test5 {
2 public static void main(String[] args) {
3 //用for循环输出100次好好学习
4 for(int i = 1;i <= 100;i++) {
5 System.out.println("第"+i+"好好学习");
6 }
7 }
8 }
3、循环输入某同学S1结业考试的5门课成绩,并计算平均分
1 import java.util.Scanner;
2 public class Test6 {
3 public static void main(String[] args) {
4 //循环输入某同学S1结业考试的5门课成绩,并计算平均分
5 int sum = 0;
6 Scanner input = new Scanner(System.in);
7 System.out.print("输入学生姓名:");
8 String name = input.next();
9 for(int i = 1;i < 6;i++) {
10 System.out.print("请输入5门功课在第"+i+"门的成绩:");
11 int score = input.nextInt();
12 sum =sum + score;//sum +=score
13 }
14 double Ave = sum/5;
15 System.out.print("王紫的平均分是:" + Ave);
16 }
17 }
4、计算100以内(包括100)的偶数之和
1 public class Test8 {
2 public static void main(String[] args) {
3 //计算100以内(包括100)的偶数之和
4 int i = 0;
5 int sum = 0;
6 while(i < 100) {
7 i = i + 2;
8 sum = sum + i;
9 }
10 System.out.println("100以内偶数之和为:" + sum);
11 }
12 }
5、输入数字判断的奇偶性,并计算该数字范围内的奇数偶数之和
1 import java.util.Scanner;
2 public class Test11 {
3 public static void main(String[] args) {
4 //输入数字判断的奇偶性,并计算该数字范围内的奇数偶数之和
5 Scanner input = new Scanner(System.in);
6 System.out.println("请输入正整数:");
7 double num = input.nextDouble();
8 //判断奇偶性
9 if(num % 2 == 0) {
10 System.out.println(num + "是偶数");
11 //用for循环计算偶数之和
12 int sum = 0;
13 for(int i = 0;i < num;i +=2,sum+=i) {
14 }
15 System.out.println(num + "的偶数之和是:" + sum);
16 //判断奇偶性
17 }else if(num % 2 == 1) {
18 System.out.println(num + "是奇数");
19 //计算奇数之和
20 int sum = 1;
21 int i = 1;
22 while(i < num) {
23 i += 2;
24 sum +=i;
25 }
26 System.out.println(num + "的奇数之和是:" + sum);
27 }
28
29 }
30 }
6、打印小星星
/* L *
* * 1 1
* ** 2 2
* *** 3 3
* **** 4 4
* ***** 5 5
*
*/
1 public class Test15 {
2 public static void main(String[] args) {
3 /* L *
4 * * 1 1
5 * ** 2 2
6 * *** 3 3
7 * **** 4 4
8 * ***** 5 5
9 *
10 */
11
12 //1.外层循环控制站视的行数
13 for(int i = 1; i < 12;i ++) {
14 //2。内层循环控制每一层展示的内容
15 for(int j = 1;j <= i;j ++) {
16 System.out.print("*");
17 }
18 System.out.println();
19 }
20 }
21 }
/*
* L _ *
* * 1 4 1
* *** 2 3 3
* ***** 3 2 5
* ******* 4 1 7
* ********* 5 0 9
*
*/
1 public class Test16 {
2 public static void main(String[] args) {
3 /*
4 * L _ *
5 * * 1 4 1
6 * *** 2 3 3
7 * ***** 3 2 5
8 * ******* 4 1 7
9 * ********* 5 0 9
10 *
11 */
12
13 //1.控制总行数
14 for(int i = 1;i <=25;i ++) {
15 //2.处理空格,空格= 总行数 - 行号
16 for(int j = 1;j <= 25 - i;j ++) {
17 System.out.print(' ');
18 }
19
20 //3.处理星星 星星个数是当前行号*2 - 1
21 for(int k = 1;k <= i*2 - 1;k ++) {
22 System.out.print("*");
23 }
24 System.out.println();
25
26 }
27 }
28 }