tip:[start]學(xué)習(xí)編程語(yǔ)言語(yǔ)法是次要的,思維是主要的。如何把頭腦中的想法變成簡(jiǎn)潔的代碼,至關(guān)重要?!Z學(xué)燦tip:[end]
- 學(xué)習(xí)循環(huán)語(yǔ)句只需要抓住一點(diǎn):代碼執(zhí)行順序!
while循環(huán)
- 可以簡(jiǎn)單理解為循環(huán)版的if語(yǔ)句。if語(yǔ)句是判斷一次,如果條件成立,則執(zhí)行后面的語(yǔ)句;while是每次判斷,如果成立,則執(zhí)行循環(huán)體中的語(yǔ)句,否則停止。
public class Main {
public static void main(String[] args) {
int i = 0;
while (i < 10) {
System.out.println(i);
i ++ ;
}
}
}
- 練習(xí):求1~100中所有數(shù)的立方和。
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);
}
}
- 練習(xí):求斐波那契數(shù)列的第n項(xiàng)。
- 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) { // 會(huì)存儲(chǔ)f(n), f(n+1)
int c = a + b; //f(3)
a = b; // f(2)
b = c; // f(3)
i ++ ; // 準(zhǔn)備計(jì)算f(4)
}
System.out.println(a);
}
}
- 死循環(huán):循環(huán)永久執(zhí)行,無(wú)法結(jié)束。我們要避免寫出死循環(huán)。
public class Main {
public static void main(String[] args) {
int x = 1;
while (x == 1)
System.out.println("!");
}
}
do while循環(huán)
-
do while循環(huán)不常用。
-
do while語(yǔ)句與while語(yǔ)句非常相似。唯一的區(qū)別是,do while語(yǔ)句限制循環(huán)體后檢查條件。不管條件的值如何,我們都要至少執(zhí)行一次循環(huán)。
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循環(huán)
-
基本思想:把控制循環(huán)次數(shù)的變量從循環(huán)體中剝離。
-
init-statement
可以是聲明語(yǔ)句、表達(dá)式、空語(yǔ)句,一般用來(lái)初始化循環(huán)變量; -
condition
是條件表達(dá)式,和while中的條件表達(dá)式作用一樣;可以為空,空語(yǔ)句表示true; -
expression
一般負(fù)責(zé)修改循環(huán)變量,可以為空。
for (init-statement; condition; expression) {
statement
}
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i ++ ) { // 循環(huán)體中只有一條語(yǔ)句時(shí),可以不加大括號(hào)
System.out.println(i);
}
}
}
- 練習(xí):求1~100中所有數(shù)的立方和。
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);
}
}
- 練習(xí):求斐波那契數(shù)列的第n項(xiàng)。
- 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
可以定義多個(gè)變量,expression
也可以修改多個(gè)變量。 - 例如求 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);
}
}
跳轉(zhuǎn)語(yǔ)句
break
-
可以提前從循環(huán)中退出,一般與if語(yǔ)句搭配。
-
例題:判斷一個(gè)大于1的數(shù)是否是質(zhì)數(shù):
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
-
可以直接跳到當(dāng)前循環(huán)體的結(jié)尾。作用與if語(yǔ)句類似。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-750695.html
-
例題:求1~100中所有偶數(shù)的和。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-750695.html
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);
}
}
多層循環(huán)
- 將1~100打印到一個(gè)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();
}
}
}
- 練習(xí):打印1~100中的所有質(zhì)數(shù)
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);
}
}
}
到了這里,關(guān)于Java-03循環(huán)語(yǔ)句的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!