我是靠谱客的博主 帅气黑猫,这篇文章主要介绍java基础知识点02_流程控制,现在分享给大家,希望可以做个参考。

java基础知识点02_流程控制


用户交互类Scanner:

复制代码
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
next()方法: 从有效字符开始,以空白作为结束符,所以不能得到带空格的字符串 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入:"); String str = scanner.next(); System.out.println(str); scanner.close(); } 输入内容:Hello BLU 输出结果:Hello nextLine()方法: 以Enter为结束符,可以得到含空格的字符串 public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("输入:"); String str = scanner.nextLine(); System.out.println(str); scanner.close(); } 输入内容:Hello BLU 输出结果:Hello BLU

顺序结构:java的基本结构,语句一条条按顺序执行。

选择结构:

复制代码
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
47
48
49
50
51
52
53
54
55
56
57
58
if单选择结构: if(name.equals("BLU")){ System.out.println(name); } if双选择结构: if(score>=60)){ System.out.println("及格"); }else { System.out.println("不及格"); } if多选择结构: if(score==100)){ System.out.println("满分"); }else if(score>=90 && score<100) { System.out.println("优秀"); }else if(score>=75 && score<90) { System.out.println("良好"); }else if(score>=60 && score<75) { System.out.println("及格"); }else if(score>=0 && score<60){ System.out.println("不及格"); }else { System.out.println("不合法成绩"); } 嵌套if结构: if(score>=0 && score<=100)){ if(score==100){ System.out.println("满分"); }else if(score>=90){ System.out.println("优秀"); }else if(score>=75){ System.out.println("良好"); }else if(score>=60){ System.out.println("及格"); }else { System.out.println("不及格"); } }else { System.out.println("不合法成绩"); } switch case 多选择结构: switch(year){ case 2019: System.out.println("去年"); break; case 2020: System.out.println("今年"); break; case 2021: System.out.println("明年"); default: System.out.println("其他年份"); }

循环结构:

复制代码
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
47
48
49
while循环输出1-100(先判断后执行)int i=0; while(i<100){ i++; System.out.println(i); } do..while循环输出1-100(先执行后判断,至少会执行一次): int i=0; do{ i++; System.out.println(i); }while(i<=100); for循环(让循环结构更简单,每次执行循环前先判断条件i<=100,执行一次循环后再i++: for(int i=1;i<=100;i++){ System.out.println(i); } 增强for循环(主要用于数组和集合的遍历): int[] numbers = {1,2,3,4,5,6,7}; for(int x : numbers){ System.out.println(x); } break终止循环: int i = 0; while (i<=100){ i++; if (i==30){ break; } System.out.println(i); } continue终止一次循环: int i = 0; while (i<=100){ i++; if (i==30){ continue; } System.out.println(i); }

最后

以上就是帅气黑猫最近收集整理的关于java基础知识点02_流程控制的全部内容,更多相关java基础知识点02_流程控制内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部