【原】Java學習筆記007 - 流程控制。本站提示廣大學習愛好者:(【原】Java學習筆記007 - 流程控制)文章只能為提供參考,不一定能成為您想要的結果。以下是【原】Java學習筆記007 - 流程控制正文
1 package cn.temptation;
2
3 public class Sample01 {
4 public static void main(String[] args) {
5 // for循環
6 // 格式
7 // for (int i = 初始值; i < value; i++) {
8 //
9 // }
10 // 口訣:左初值、右步長、條件在中間、處理在內部
11 // 1、左初值:聲明一個變量並給變量賦值
12 // 2、右步長:控制左邊聲明的變量的變化幅度,為了讓循環有跳出的可能
13 // 3、條件在中間:中間部分是循環的條件,當條件滿足時,執行循環體中的語句;不滿足時就不執行
14
15 // 需求:使用for循環輸出1~5
16 for (int i = 1; i <= 5; i++) {
17 System.out.println(i);
18 }
19
20 // for循環執行順序:(面試時可能被問到的問題)
21 // 可以結合調試模式查看變量的值的變化,得到如下結論:
22 // 1、第一次循環----->先做左初值的變量聲明 和 賦初始化值
23 // 2、 ----->判斷條件是否滿足
24 // 3、 ----->進入到循環體中執行語句
25 // 4、 ----->執行完循環體中的語句後,再做右步長,控制左邊變量的變化幅度
26 // 5、第二次循環----->從這次開始就不再做左初值,直接判斷條件是否滿足
27 // 6、 ----->進入到循環體中執行語句
28 // 7、 ----->執行完循環體中的語句後,再做右步長,控制左邊變量的變化幅度
29 // 8、...
30 // 9、 ----->直到某次循環條件不再滿足,跳出循環
31 }
32 }
1 package cn.temptation;
2
3 public class Sample02 {
4 public static void main(String[] args) {
5 // 需求:分別使用while循環 和 for循環輸出 1~5
6
7 // while循環
8 int i = 1;
9 while (i <= 5) {
10 System.out.println(i);
11 i++;
12 }
13
14 // for循環
15 for (int j = 1; j <= 5; j++) {
16 System.out.println(j);
17 }
18
19 // 對比一下while循環 和 for循環
20 // 發現 while 循環 和 for 循環 差不多,只是把多行語句合並到一行裡了
21 }
22 }
1 package cn.temptation;
2
3 public class Sample03 {
4 public static void main(String[] args) {
5 // 需求:使用for循環輸出1~100
6 // 需求:使用for循環計算1+2+...+100
7
8 // 左初值:初始值由開發人員自己設置
9
10 // 寫法1
11 for (int i = 1; i <= 100; i++) {
12 System.out.println(i);
13 }
14
15 // 寫法2
16 for (int i = 0; i < 100; i++) {
17 System.out.println(i + 1);
18 }
19
20 // 寫法3
21 for (int i = -1; i < 99; i++) {
22 System.out.println(i + 2);
23 }
24
25 // 注意:第11行、第16行、第21行都使用了變量i,之前講變量名不能重復,這裡為何沒有提示語法錯誤?
26 // 因為變量的作用范圍(作用域),這裡的for循環中定義的變量,只能在該循環中可以使用,超出了該循環范圍就無法使用了
27 // 語法錯誤:i cannot be resolved to a variable
28 // System.out.println(i);
29
30 // 定義總和的變量
31 int sum = 0;
32
33 for (int i = 1; i <= 100; i++) {
34 sum += i;
35 }
36
37 System.out.println("1+2+...+100=" + sum);
38 }
39 }
1 package cn.temptation;
2
3 public class Sample04 {
4 public static void main(String[] args) {
5 // 需求:使用for循環列出1~100之間的奇數
6
7 // 寫法1、奇數的間隔為2
8 // for (int i = 1; i < 100; i += 2) {
9 // System.out.println(i);
10 // }
11 for (int i = 1; i < 100; i = i + 2) {
12 System.out.println(i);
13 }
14
15 // 寫法2、奇數是和2求余為1的數
16 for (int i = 1; i <= 100; i++) {
17 if (i % 2 == 1) {
18 System.out.println(i);
19 }
20 }
21 }
22 }
1 package cn.temptation;
2
3 public class Sample05 {
4 public static void main(String[] args) {
5 // 需求:計算1~100之間的偶數的和
6 // int sum = 0;
7
8 // 寫法1、偶數的間隔為2
9 // for (int i = 0; i <= 100; i += 2) {
10 // sum += i;
11 // }
12
13 // 寫法2、偶數是和2求余數為0的數
14 // for (int i = 0; i <= 100; i++) {
15 // if (i % 2 == 0) {
16 // sum += i;
17 // }
18 // }
19
20 // System.out.println("1~100之間的偶數的和為:" + sum);
21
22 // 作為和的變量sum也在for循環中定義,如何寫?
23 // 變量sum在循環結構的頭部進行聲明和賦初始化值,在循環體內可以使用它,但是在循環體外無法訪問它
24 // for (int i = 0, sum = 0; i <= 100; i += 2) {
25 // sum += i;
26 // // 添加選擇結構,讓循環在最後一次輸出結果
27 // if (i == 100) {
28 // System.out.println("1~100之間的偶數的和為:" + sum);
29 // }
30 // }
31
32 // System.out.println("1~100之間的偶數的和為:" + sum);
33
34 // 注意:如下寫法,變量sum會在每次循環時重新設置為0,也就不會保存之前循環操作的結果
35 // 這也從側面說明左初值中的內容只在第一次循環時執行,後續循環時都不會再執行
36 for (int i = 0; i <= 100; i++) {
37 int sum = 0;
38 sum += i;
39 System.out.println("1~100之間的偶數的和為:" + sum);
40 }
41 }
42 }
1 package cn.temptation;
2
3 public class Sample06 {
4 public static void main(String[] args) {
5 // 需求:使用for循環統計100~999之間的水仙花數並計算有多少個?
6
7 // 規則:以三位數字為例,數字的值 = (百位上的數字 * 百位上的數字 * 百位上的數字)
8 // + (十位上的數字 * 十位上的數字 * 十位上的數字)
9 // + (個位上的數字 * 個位上的數字 * 個位上的數字),則稱為是一個水仙花數
10 // 153 = 1 * 1 * 1 + 5 * 5 * 5 + 3 * 3 * 3
11
12 // 定義統計用個數變量
13 int count = 0;
14
15 for (int number = 100; number <= 999; number++) {
16 // 獲取個位數字
17 int i = number / 1 % 10;
18 // 獲取十位數字
19 int j = number / 10 % 10;
20 // 獲取百位數字
21 int k = number / 100 % 10;
22
23 if (number == i * i * i + j * j * j + k * k * k) {
24 // 滿足規則的數字輸出
25 System.out.println(number);
26
27 count++;
28 }
29 }
30
31 System.out.println("100~999之間的水仙花數並計算有" + count + "個");
32 }
33 }
1 package cn.temptation;
2
3 public class Sample07 {
4 public static void main(String[] args) {
5 // 需求:使用for循環計算10的階乘
6 // 階乘:10! = 10 * 9 * 8 * 7 * ... * 1
7
8 // 定義結果變量
9 int result = 1;
10
11 // 寫法1
12 // for (int i = 1; i <= 10; i++) {
13 // result *= i;
14 // }
15
16 // 寫法2
17 for (int i = 10; i >= 1; i--) {
18 result *= i;
19 }
20
21 System.out.println("10的階乘為:" + result);
22
23 // 注意:編程動手之前,先考慮一下程序執行結果的數值范圍,選擇合適的數據類型
24 }
25 }
1 package cn.temptation;
2
3 public class Sample08 {
4 public static void main(String[] args) {
5 // 需求:使用for循環解決百錢百雞問題:母雞一只5元,公雞一只3元,小雞三只1元,有100元,如何買到100只雞?母雞多少只?公雞多少只?小雞多少只?(母雞、公雞、小雞都有)
6
7 // 思路:
8 // 從需求中得到一些等式
9 // 母雞數量 + 公雞數量 + 小雞數量 = 100
10 // 母雞單價 * 母雞數量 + 公雞單價 * 公雞數量 + 小雞單價 * 小雞數量 = 100
11 // 小雞數量是3的倍數
12
13 // 運用"窮舉法"
14 // 如果買1只母雞,再買1只公雞,還能買多少只小雞
15 // 如果買1只母雞,再買2只公雞,還能買多少只小雞
16 // ...
17 // 如果買2只母雞,再買1只公雞,還能買多少只小雞
18 // 如果買2只母雞,再買2只公雞,還能買多少只小雞
19 // ...
20
21 System.out.println("使用for循環求解百錢百雞問題:");
22
23 for (int i = 1; i < 20; i++) { // 計算母雞
24 for (int j = 1; j < 33; j++) { // 計算公雞
25 // 小雞數量
26 int k = 100 - i - j;
27
28 if ((5 * i + 3 * j + k / 3 == 100) && (k % 3 == 0)) {
29 System.out.println("母雞:" + i + "只,公雞:" + j + "只,小雞:" + k + "只");
30 }
31 }
32 }
33 }
34 }
1 package cn.temptation;
2
3 public class Sample09 {
4 public static void main(String[] args) {
5 // 需求:打印顯示 4 * 4 正方形的點陣
6
7 // 寫法1、最原始的寫法
8 // System.out.println("****");
9 // System.out.println("****");
10 // System.out.println("****");
11 // System.out.println("****");
12
13 // 寫法2、借助於 \r \n實現換行的操作
14 // System.out.println("****\r\n****\r\n****\r\n****");
15
16 // 思路:
17 // 1、在一行中把*號輸出4次,形成****
18 // 2、再在3個不同的行上重復步驟1的操作
19
20 // 寫法3、分步循環操作
21 // for (int i = 0; i < 4; i++) {
22 // System.out.print("*");
23 // }
24 // System.out.println();
25 // for (int i = 0; i < 4; i++) {
26 // System.out.print("*");
27 // }
28 // System.out.println();
29 // for (int i = 0; i < 4; i++) {
30 // System.out.print("*");
31 // }
32 // System.out.println();
33 // for (int i = 0; i < 4; i++) {
34 // System.out.print("*");
35 // }
36 // System.out.println();
37
38 // 寫法4、針對寫法3的改進,因為有發現相同的事情重復做,自然考慮使用循環
39 // 循環的嵌套
40 // 內部的操作時在一行中循環輸出4個星號,把內部的操作作為一個整體,在外部再重復內部的這個操作3次
41
42 // 外部循環操作:重復內部的這個操作4次
43 for (int j = 0; j < 4; j++) {
44 // 內部循環操作:在一行中循環輸出4個星號
45 for (int i = 0; i < 4; i++) {
46 System.out.print("*");
47 }
48 System.out.println();
49 }
50 }
51 }
1 package cn.temptation;
2
3 public class Sample10 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *
7 // **
8 // ***
9 // ****
10 // *****
11
12 // 思路:找規律
13 // 1、第1行1個星號
14 // 2、第2行2個星號
15 // 3、第3行3個星號
16 // 4、第4行4個星號
17 // 5、第5行5個星號
18
19 // 分步操作
20 // for (int i = 0; i < 1; i++) {
21 // System.out.print("*");
22 // }
23 // // 換行
24 // System.out.println();
25 // for (int i = 0; i < 2; i++) {
26 // System.out.print("*");
27 // }
28 // // 換行
29 // System.out.println();
30 // for (int i = 0; i < 3; i++) {
31 // System.out.print("*");
32 // }
33 // // 換行
34 // System.out.println();
35 // for (int i = 0; i < 4; i++) {
36 // System.out.print("*");
37 // }
38 // // 換行
39 // System.out.println();
40 // for (int i = 0; i < 5; i++) {
41 // System.out.print("*");
42 // }
43 // // 換行
44 // System.out.println();
45
46 // 看到有重復的操作,考慮使用循環
47 // 看見有變化的量:1、2、3、4、5,考慮使用變量
48 // 通過找規律,我們發現這變量的值和其所在的行號一致
49 // 行號是什麼?行號就是外層循環操作的當前次操作的數字,且這個數字是提供給內層循環來使用的數字(用於循環條件的控制)
50
51 // 寫法1
52 // 外層循環
53 for (int i = 1; i <= 5; i++) {
54 // 內層循環
55 for (int j = 0; j < i; j++) {
56 System.out.print("*");
57 }
58 // 換行
59 System.out.println();
60 }
61
62 // 寫法2
63 for (int i = 0; i < 5; i++) {
64 for (int j = 0; j <= i; j++) {
65 System.out.print("*");
66 }
67 // 換行
68 System.out.println();
69 }
70 }
71 }
1 package cn.temptation;
2
3 public class Sample11 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *****
7 // ****
8 // ***
9 // **
10 // *
11
12 // 思路:找規律
13 // 1、第1行5個星號
14 // 2、第2行4個星號
15 // 3、第3行3個星號
16 // 4、第4行2個星號
17 // 5、第5行1個星號
18
19 // 寫法1
20 for (int i = 0; i < 5; i++) {
21 for (int j = 4; j >= i; j--) {
22 System.out.print("*");
23 }
24 // 換行
25 System.out.println();
26 }
27
28 // 寫法2
29 for (int i = 0; i < 5; i++) {
30 for (int j = 0; j < 5 - i; j++) {
31 System.out.print("*");
32 }
33 System.out.println();
34 }
35 }
36 }
1 package cn.temptation;
2
3 public class Sample12 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *
7 // **
8 // ***
9 // ****
10 // *****
11
12 // 思路:
13 // 1、星號還是打印一樣的星號
14 // 2、在每一行輸出星號之前,先輸出了空格
15 // 3、一個較復雜的問題分解為兩個簡單的問題來處理:分解為每行中先輸出空格,再輸出星號
16
17 for (int i = 0; i < 5; i++) {
18 // 輸出空格的處理
19 for (int j = 0; j < 4 - i; j++) {
20 System.out.print(" ");
21 }
22
23 // 輸出星號的處理
24 for (int j = 0; j <= i; j++) {
25 System.out.print("*");
26 }
27 System.out.println();
28 }
29 }
30 }
1 package cn.temptation;
2
3 public class Sample13 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *****
7 // ****
8 // ***
9 // **
10 // *
11
12 for (int i = 0; i < 5; i++) {
13 // 打印空格
14 for (int j = 0; j < i; j++) {
15 System.out.print(" ");
16 }
17
18 // 打印星號
19 for (int j = 0; j < 5 - i; j++) {
20 System.out.print("*");
21 }
22
23 // 換行
24 System.out.println();
25 }
26 }
27 }
1 package cn.temptation;
2
3 public class Sample14 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *
7 // ***
8 // *****
9 // *******
10 // *********
11
12 for (int i = 0; i < 5; i++) {
13 // 打印空格
14 for (int j = 0; j < 4 - i; j++) {
15 System.out.print(" ");
16 }
17
18 // 打印星號
19 for (int j = 1; j <= i * 2 + 1; j++) {
20 System.out.print("*");
21 }
22
23 // 換行
24 System.out.println();
25 }
26 }
27 }
1 package cn.temptation;
2
3 public class Sample15 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *
7 // **
8 // ***
9 // ****
10 // *****
11 // ****
12 // ***
13 // **
14 // *
15
16 // 思路:不妨把這個問題分解為打印上部分 和 下部分
17
18 // 打印上部分
19 for (int i = 0; i < 5; i++) {
20 for (int j = 0; j <= i; j++) {
21 System.out.print("*");
22 }
23 // 換行
24 System.out.println();
25 }
26
27 // 打印下部分
28 for (int i = 0; i < 4; i++) {
29 for (int j = 0; j < 4 - i; j++) {
30 System.out.print("*");
31 }
32 // 換行
33 System.out.println();
34 }
35 }
36 }
1 package cn.temptation;
2
3 public class Sample16 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *
7 // **
8 // ***
9 // ****
10 // *****
11 // ****
12 // ***
13 // **
14 // *
15
16 // 思路:不妨把這個問題分解為打印上部分 和 下部分
17
18 // 打印上部分
19 for (int i = 0; i < 5; i++) {
20 // 打印空格
21 for (int j = 0; j < 4 - i; j++) {
22 System.out.print(" ");
23 }
24 // 打印星號
25 for (int j = 0; j <= i; j++) {
26 System.out.print("*");
27 }
28 // 換行
29 System.out.println();
30 }
31
32 // 打印下部分
33 for (int i = 0; i < 4; i++) {
34 // 打印空格
35 for (int j = 0; j <= i; j++) {
36 System.out.print(" ");
37 }
38 // 打印星號
39 for (int j = 0; j < 4 - i; j++) {
40 System.out.print("*");
41 }
42 // 換行
43 System.out.println();
44 }
45 }
46 }
1 package cn.temptation;
2
3 public class Sample17 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *
7 // ***
8 // *****
9 // *******
10 // *********
11 // *******
12 // *****
13 // ***
14 // *
15
16 // 思路:不妨把這個問題分解為打印上部分 和 下部分
17
18 // 打印上部分
19 for (int i = 0; i < 5; i++) {
20 // 打印空格
21 for (int j = 0; j < 4 - i; j++) {
22 System.out.print(" ");
23 }
24
25 // 打印星號
26 for (int j = 1; j <= i * 2 + 1; j++) {
27 System.out.print("*");
28 }
29
30 // 換行
31 System.out.println();
32 }
33
34 // 打印下部分
35 for (int i = 0; i < 4; i++) {
36 // 打印空格
37 for (int j = 0; j <= i; j++) {
38 System.out.print(" ");
39 }
40
41 // 打印星號
42 for (int j = 0; j <= 7 - i * 2 - 1; j++) {
43 System.out.print("*");
44 }
45
46 // 換行
47 System.out.println();
48 }
49 }
50 }
1 package cn.temptation;
2
3 public class Sample18 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印如下圖形
6 // *
7 // * *
8 // * *
9 // * *
10 // * *
11 // * *
12 // * *
13 // * *
14 // *
15
16 // 思路:不妨把這個問題分解為打印上部分 和 下部分
17
18 // 打印上部分
19 for (int i = 0; i < 5; i++) {
20 // 打印空格
21 for (int j = 0; j < 4 - i; j++) {
22 System.out.print(" ");
23 }
24
25 // 打印星號
26 System.out.print("*");
27
28 if (i != 0) { // 第一行不執行
29 // 打印空格
30 for (int j = 0; j < 2 * i - 1; j++) {
31 System.out.print(" ");
32 }
33
34 // 打印星號
35 System.out.print("*");
36 }
37
38
39 // 換行
40 System.out.println();
41 }
42
43 // 打印下部分
44 for (int i = 0; i < 4; i++) {
45 // 打印空格
46 for (int j = 0; j <= i; j++) {
47 System.out.print(" ");
48 }
49
50 // 打印星號
51 System.out.print("*");
52
53 if (i != 3) { // 最後一行不執行
54 // 打印空格
55 for (int j = 0; j < 5 - 2 * i; j++) {
56 System.out.print(" ");
57 }
58
59 // 打印星號
60 System.out.print("*");
61 }
62
63 // 換行
64 System.out.println();
65 }
66 }
67 }
1 package cn.temptation;
2
3 public class Sample19 {
4 public static void main(String[] args) {
5 // 需求:使用for循環打印九九乘法口訣表
6
7 // 思路:
8 // 1、把沒有遇見過的問題轉換為遇見過的問題
9 // 2、只要把星號替換為算術表達式即可
10 // 3、使用"\t"實現制表符的效果
11
12 System.out.println("-----九九乘法口訣表-----");
13
14 for (int i = 1; i <= 9; i++) {
15 for (int j = 1; j <= i; j++) {
16 // System.out.print("*" + "\t");
17 System.out.print(j + "*" + i + "=" + (i * j) + "\t");
18 }
19 // 換行
20 System.out.println();
21 }
22 }
23 }
1 package cn.temptation;
2
3 public class Sample20 {
4 public static void main(String[] args) {
5 // break語句
6 // 1、switch語句中,結合case條件 或是 default語句,起的是跳出選擇結構的作用
7 // 2、break用在循環中,起的跳出循環結構的作用,在某次循環執行過程中執行到break語句後跳出了循環,break之後的語句是執行不到的
8
9 // int i = 2;
10
11 // switch (i) {
12 // case 1:
13 // System.out.println("i的值為1");
14 // break;
15 // case 2:
16 // System.out.println("i的值為2");
17 // break;
18 // default:
19 // break;
20 // }
21
22 // while (i < 5) {
23 // System.out.println(i);
24 // i++;
25 // break;
26 // // 語法錯誤:Unreachable code
27 // System.out.println("這是break語句之後的語句");
28 // }
29
30 // Dead Code 描述右步長部分為死代碼
31 // 再次從側面證明了右步長是在循環體內的語句執行結束後去執行的
32 for (int j = 0; j < 3; j++) {
33 System.out.println(j);
34 break;
35 // 語法錯誤:Unreachable code
36 // System.out.println("這是break語句之後的語句");
37 }
38
39 // 注意:下句也是break語句後的語句,但是不受break語句的影響,因為break語句是for循環中的語句,是跳出循環的操作
40 System.out.println("如果看到這句,說明循環結束了");
41 }
42 }
1 package cn.temptation;
2
3 public class Sample21 {
4 public static void main(String[] args) {
5 // continue語句
6 // 1、continue語句不能用在循環以外的結構中
7 // 2、循環中的continue語句之後的語句會提示語法錯誤:無法到達的代碼
8 // 3、continue語句表示跳出當前當次循環
9
10 int i = 2;
11
12 if (i == 3) {
13 System.out.println("i等於3");
14 // 語法錯誤:continue cannot be used outside of a loop
15 // continue;
16 }
17
18 // while (i < 5) {
19 // System.out.println(i);
20 // continue;
21 // // 語法錯誤:Unreachable code
22 //// System.out.println("continue語句之後的語句");
23 // }
24
25 // for (int j = 0; j < 5; j++) {
26 // if (j == 2) {
27 // continue;
28 // }
29 // System.out.println(j);
30 // }
31
32 // 對比break語句 和 continue語句的效果
33 // 結論:break語句跳出循環(後續循環條件滿足也不執行了);continue語句跳出當前當次循環
34 for (int j = 0; j < 5; j++) {
35 if (j == 2) {
36 break;
37 }
38 System.out.println(j);
39 }
40 }
41 }
1 package cn.temptation;
2
3 public class Sample22 {
4 public static void main(String[] args) {
5 // 循環中死循環寫法的總結
6
7 // while (true) {
8 // System.out.println("while死循環的寫法");
9 // }
10
11 // do {
12 // System.out.println("do...while死循環的寫法");
13 // } while (true);
14
15 // for (;;) {
16 // System.out.println("for死循環的寫法");
17 // }
18
19 // 根據while 和 for 死循環的寫法,稍作變形,側面證明while結構 和 for結構是一樣的
20 int i = 2;
21
22 // while (i < 5) {
23 // System.out.println(i);
24 // i++;
25 // }
26
27 for (; i < 5;) {
28 System.out.println(i);
29 i++;
30 }
31 }
32 }