tip:[start]學習編程語言語法是次要的,思維是主要的。如何把頭腦中的想法變成簡潔的代碼,至關重要。——閆學燦tip:[end] 學習迴圈語句只需要抓住一點:代碼執行順序! while迴圈 可以簡單理解為迴圈版的if語句。if語句是判斷一次,如果條件成立,則執行後面的語句;while是每次判斷 ...
tip:[start]學習編程語言語法是次要的,思維是主要的。如何把頭腦中的想法變成簡潔的代碼,至關重要。——閆學燦tip:[end]
- 學習迴圈語句只需要抓住一點:代碼執行順序!
while迴圈
- 可以簡單理解為迴圈版的if語句。if語句是判斷一次,如果條件成立,則執行後面的語句;while是每次判斷,如果成立,則執行迴圈體中的語句,否則停止。
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
System.out.println(i);
i ++ ;
}
}
}
- 練習:求1~100中所有數的立方和。
public class Main {
public static void main(String[] args) {
int i = 1, sum = 0;
while (i <= 100) {
sum += i * i * i;
i ++ ;
}
System.out.println(sum);
}
}
- 練習:求斐波那契數列的第n項。
- f(1) = 1
- f(2) = 1
- f(3) = 2
- ...
- f(n) = f(n-1) + f(n-2)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 1, b = 1, i = 1; // f(1), f(2)
while (i < n) { // 會存儲f(n), f(n+1)
int c = a + b; //f(3)
a = b; // f(2)
b = c; // f(3)
i ++ ; // 準備計算f(4)
}
System.out.println(a);
}
}
- 死迴圈:迴圈永久執行,無法結束。我們要避免寫出死迴圈。
public class Main {
public static void main(String[] args) {
int x = 1;
while (x == 1)
System.out.println("!");
}
}
do while迴圈
-
do while迴圈不常用。
-
do while語句與while語句非常相似。唯一的區別是,do while語句限制迴圈體後檢查條件。不管條件的值如何,我們都要至少執行一次迴圈。
public class Main {
public static void main(String[] args) {
int x = 1;
while (x < 1) {
System.out.println("x!");
}
int y = 1;
do {
System.out.println("y!");
} while (y < 1);
}
}
for迴圈
-
基本思想:把控制迴圈次數的變數從迴圈體中剝離。
-
init-statement
可以是聲明語句、表達式、空語句,一般用來初始化迴圈變數; -
condition
是條件表達式,和while中的條件表達式作用一樣;可以為空,空語句表示true; -
expression
一般負責修改迴圈變數,可以為空。
for (init-statement; condition; expression) {
statement
}
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i ++ ) { // 迴圈體中只有一條語句時,可以不加大括弧
System.out.println(i);
}
}
}
- 練習:求1~100中所有數的立方和。
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i ++ )
sum += i * i * i;
System.out.println(sum);
}
}
- 練習:求斐波那契數列的第n項。
- f(1) = 1
- f(2) = 1
- f(3) = 2
- ...
- f(n) = f(n-1) + f(n-2)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 1, b = 1;
for (int i = 1; i < n; i ++ ) {
int c = a + b;
a = b;
b = c;
}
System.out.println(a);
}
}
init-statement
可以定義多個變數,expression
也可以修改多個變數。- 例如求 1 * 10 + 2 * 9 + 3 * 8 + 4 * 7 + 5 * 6:
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1, j = 10; i < j; i ++, j -- ) {
sum += i * j;
}
System.out.println(sum);
}
}
跳轉語句
break
-
可以提前從迴圈中退出,一般與if語句搭配。
-
例題:判斷一個大於1的數是否是質數:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean isPrime = true;
for (int i = 2; i < n; i ++ )
if (n % i == 0) {
isPrime = false;
break;
}
if (isPrime)
System.out.println("yes");
else
System.out.println("no");
}
}
continue
-
可以直接跳到當前迴圈體的結尾。作用與if語句類似。
-
例題:求1~100中所有偶數的和。
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i ++ ) {
if (i % 2 == 1) continue;
sum += i;
}
System.out.println(sum);
}
}
多層迴圈
- 將1~100列印到一個10 * 10的矩陣中:
public class Main {
public static void main(String[] args) {
for (int i = 0, k = 1; i < 10; i ++ ) {
for (int j = 0; j < 10; j ++, k ++ ) {
System.out.printf("%d ", k);
}
System.out.println();
}
}
}
- 練習:列印1~100中的所有質數
public class Main {
public static void main(String[] args) {
for (int i = 2; i <= 100; i ++ ) {
boolean isPrime = true;
for (int j = 2; j < i; j ++ ) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime)
System.out.println(i);
}
}
}