1.选择结构
- if语句
格式1: if (关系表达式) {
语句体;
}
其它语句;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public static void main(String[] args) { System.out.println("开始"); //定义两个变量 int a = 10; int b = 20; //需求:判断a和b的值是否相等,如果相等,就在控制台输出:a等于b if (a == b) { System.out.println("a等于b"); } //需求:判断a和c的值是否相等,如果相等,就在控制台输出:a等于c int c = 10; if (a == c) { System.out.println("a等于c"); } System.out.println("结束"); }
运行结果
- 格式2:
if (关系表达式)
{ 语句体1;
} else {
语句体2;
}
其它语句;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13public static void main(String[] args) { System.out.println("开始"); //定义两个变量 int a = 10; int b = 20; b = 5; // 需求:判断a是否大于b,如果是,在控制台输出:a的值大于b,否则,在控制台输出:a的值不大于b if (a > b) { System.out.println("a的值大于b"); } else { System.out.println("a的值不大于b"); } System.out.println("结束"); }
运行结果
-格式3
if (关系表达式1) {
语句体1;
} else if (关系表达式2) {
语句体2;
}
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public static void main(String[] args) { //小明的考试成绩未知,可以使用键盘录入的方式获取值 Scanner sc = new Scanner(System.in); System.out.println("请输入一个分数:"); int score = sc.nextInt(); //由于奖励种类较多,属于多种判断,采用if...else...if格式实现 // 为每种判断设置对应的条件 // 为每种判断设置对应的奖励 // 数据测试:正确数据,边界数据,错误数据 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 { System.out.println("胖揍一顿"); } }
- 选择结构switch语句
switch(表达式) {
case 常量值1:
语句体1;
break;
case 常量值2:
语句体2;
break;
…
default:
语句体n+1;
break;
}
//其它语句
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47public static void main(String[] args) { //定义月份变量,判断季节 int month = 6; //switch语句实现选择 switch (month) { 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; case 8: System.out.println("夏季"); break; case 9: System.out.println("秋季"); break; case 10: System.out.println("秋季"); break; case 11: System.out.println("秋季"); break; case 12: System.out.println("冬季"); break; default: System.out.println("你输入的月份数字有误"); break; } }
2.循环结构
概述
循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个
循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循环,否则循环将一直执行下去,形成死循环。
- for循环
格式:
for(初始化表达式①; 布尔表达式②; 步进表达式④){
循环体③
}
//其它语句
复制代码
1
2
3
4
5
6
7
8
9
10
11
12public static void main(String[] args) { //需求:输出数据1-5 for (int i = 1; i <= 5; i++) { System.out.println(i); } System.out.println("--------"); //需求:输出数据5-1 for (int i = 5; i >= 1; i--) { System.out.println(i); } }
运行结果
2. while循环
格式:
初始化表达式①
while(布尔表达式②){
循环体③
步进表达式④
}
//其它语句
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14public static void main(String[] args) { //for循环实现打印10次HelloWorld for (int i = 1; i <= 10; i++) { System.out.println("HelloWorld"); } //while循环实现打印10次HelloWorld //定义初始化变量 int i = 1; //循环条件<=10 while (i <= 10) { System.out.println("HelloWorld"); //步进 i++; } }
运行结果
3. do while循环
格式:
初始化表达式①
do{
循环体③
步进表达式④
}while(布尔表达式②);
//其它语句
复制代码
1
2
3
4
5
6
7
8public static void main(String[] args) { int x = 1; do { System.out.println("HelloWorld"); x++; } while (x <= 10); }
运行结果同上
- 循环跳转
break语句:
使用场景:终止switch或者循环
在选择结构switch语句中
在循环语句中
离开使用场景的存在是没有意义的
复制代码
1
2
3
4
5
6
7
8
9public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i % 5 == 0) { break; } System.out.println("HelloWorld" + i); } }
运行结果
continue语句
使用场景:结束本次循环,继续下一次的循环
只能使用在循环语句中
复制代码
1
2
3
4
5
6
7
8
9public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i % 5 == 0) { continue; } System.out.println("HelloWorld" + i); } }
运行结果
3.循环嵌套
所谓嵌套循环,是指一个循环的循环体是另一个循环。比如for循环里面还有一个for循环,就是嵌套循环。总共的循环次数=外循环次数*内循环次数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13public static void main(String[] args) { //打印2021年至2023年月份 // 年份是外循环,3年;月份是内循环,12月 for (int i = 2021; i <= 2023; i++) { for (int j = 1; j <= 12; j++) { //不换行打印星号 System.out.print(i + "年" + j + "月 "); } //内循环打印12个月后,需要一次换行 System.out.println(); } }
运行结果
最后
以上就是彩色眼神最近收集整理的关于选择结构,循环结构以及循环的嵌套的全部内容,更多相关选择结构,循环结构以及循环内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复