1. 程式人生 > 實用技巧 >零基礎學Java第八章

零基礎學Java第八章

一、迴圈流程控制語句

1.1 for迴圈的格式及基本使用

1.1.1 for迴圈語句格式:

for(初始化語句;判斷條件語句;控制條件語句) {
迴圈體語句;
}

1.1.2 執行流程

1):執行初始化語句

2):執行判斷條件語句,看其結果是true還是false
如果是false,迴圈結束。
如果是true,繼續執行。

3):執行迴圈體語句

4):執行控制條件語句

5):回到B繼續

1.1.3 執行流程for迴圈的執行流程圖

1.1.4 程式碼案例

package com.test.day03;

/*
* for迴圈語句格式:
* 		for(初始化語句;判斷條件語句;控制條件語句) {
* 			迴圈體語句;
* 		}
* 
* 		執行流程:
* 			A:執行初始化語句
* 			B:執行判斷條件語句,看結果是true還是false
* 				如果是true,就繼續執行
* 				如果是false,就結束迴圈
* 			C:執行迴圈體語句
* 			D:執行控制條件語句
* 			E:回到B繼續
* 
* 需求:
* 		在控制檯輸出10次”HelloWorld”的案例。
*/
public class ForDemo {
 public static void main(String[] args) {
 	// 原始寫法
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("HelloWorld");
 	System.out.println("-------------------------");

 	// 用迴圈改進
 	for (int x = 1; x <= 10; x++) {
 		System.out.println("HelloWorld");
 	}
 }
}

1.2 for迴圈的練習

1.2.1 for迴圈實現獲取指定範圍資料

示例程式碼。僅供參考

package com.test.day03;

/*
* 需求:獲取資料1-5和5-1
*/

public class ForDemo {
 public static void main(String[] args) {
 	// 原始做法
 	System.out.println(1);
 	System.out.println(2);
 	System.out.println(3);
 	System.out.println(4);
 	System.out.println(5);
 	System.out.println("-------------");

 	// 用迴圈改進
 	for (int x = 1; x <= 5; x++) {
 		System.out.println(x);
 	}
 	System.out.println("-------------");

 	// 1-5的資料我們獲取到了,如何獲取5-1呢?
 	for (int x = 5; x >= 1; x--) {
 		System.out.println(x);
 	}
 }

}

1.2.2 for迴圈實現1-5之間資料求和

示例程式碼。僅供參考

package com.test.day03;

/*
* 需求:求出1-5之間資料之和
* 
* 分析:
* 		A:定義求和變數,初始化值是0
* 		B:獲取1-5之間的資料,用for迴圈實現
* 		C:把每一次獲取到的資料,累加起來就可以了
* 		D:輸出求和變數即可
*/
public class ForDemo {
 public static void main(String[] args) {
 	// 定義求和變數,初始化值是0
 	int sum = 0;

 	// 獲取1-5之間的資料,用for迴圈實現
 	for (int x = 1; x <= 5; x++) {
 		// 把每一次獲取到的資料,累加起來就可以了
 		// sum = sum + x;
 		/*
 		 * 第一次:sum = 0 + 1 = 1 第二次:sum = 1 + 2 = 3 第三次:sum = 3 + 3 = 6 第四次:sum = 6 + 4 =
 		 * 10 第五次:sum = 10 + 5 = 15
 		 */
 		sum += x;
 	}

 	// 輸出求和結果
 	System.out.println("sum:" + sum);
 }
}

1.2.3 for迴圈實現1-100之間偶數和

示例程式碼。僅供參考

package com.test.day03;

/*
* 需求:求出1-100之間偶數和
* 
* 分析:
* 		A:定義求和變數,初始化值是0
* 		B:獲取1-100之間的資料,用for迴圈實現
* 		C:把獲取到的資料進行判斷,看是否是偶數
* 			如果是,就累加
* 		D:輸出求和結果
*/

public class ForDemo {
 public static void main(String[] args) {
 	// 定義求和變數,初始化值是0
 	int sum = 0;

 	// 獲取1-100之間的資料,用for迴圈實現
 	for (int x = 1; x <= 100; x++) {
 		// 把獲取到的資料進行判斷,看是否是偶數
 		if (x % 2 == 0) {
 			sum += x;
 		}
 	}

 	// 輸出求和結果
 	System.out.println("sum:" + sum);
 }

}

1.2.4 for迴圈實現在控制檯列印水仙花數

示例程式碼。僅供參考

package com.test.day03;

/*
* 需求:在控制檯輸出所有的”水仙花數”
* 
* 分析:
* 		什麼是水仙花數呢?
* 			所謂的水仙花數是指一個三位數,其各位數字的立方和等於該數本身。
*			舉例:153就是一個水仙花數。
*			153 = 1*1*1 + 5*5*5 + 3*3*3
*
*		A:三位數其實就告訴了我們水仙花數的範圍
*			100-999
*		B:如何獲取一個數據的每一個位上的數呢?
*			舉例:我有一個數據153,請問如何獲取到個位,十位,百位
*			個位:153%10 = 3;
*			十位:153/10%10 = 5;
*			百位:153/10/10%10 = 1;
*			千位:...
*			萬位:...
*		C:讓每個位上的立方和相加,並和該資料進行比較,如果相等,就說明該資料是水仙花數,在控制檯輸出
*/

public class ForDemo {
 public static void main(String[] args) {
 	// 通過迴圈獲取到每一個三位數
 	for (int x = 100; x < 1000; x++) {
 		// 獲取個位,十位,百位
 		int ge = x % 10;
 		int shi = x / 10 % 10;
 		int bai = x / 10 / 10 % 10;

 		// 讓每個位上的立方和相加,並和該資料進行比較,如果相等,就說明該資料是水仙花數,在控制檯輸出
 		if ((ge * ge * ge + shi * shi * shi + bai * bai * bai) == x) {
 			System.out.println(x);
 		}
 	}
 }

}

1.2.5 for迴圈實現統計水仙花的個數

示例程式碼。僅供參考

package com.test.day03;

/*
* 需求:統計”水仙花數”共有多少個
* 
* 分析:
* 		A:定義統計變數,初始化值是0
* 		B:獲取三位數,用for迴圈實現
* 		C:獲取三位數的個位,十位,百位
* 		D:判斷這個三位數是否是水仙花數,如果是,統計變數++
* 		E:輸出統計結果就可以了
*/

public class ForDemo {
 public static void main(String[] args) {
 	// 定義統計變數,初始化值是0
 	int count = 0;

 	// 獲取三位數,用for迴圈實現
 	for (int x = 100; x < 1000; x++) {
 		// 獲取三位數的個位,十位,百位
 		int ge = x % 10;
 		int shi = x / 10 % 10;
 		int bai = x / 10 / 10 % 10;

 		// 判斷這個三位數是否是水仙花數,如果是,統計變數++
 		if ((ge * ge * ge + shi * shi * shi + bai * bai * bai) == x) {
 			count++;
 		}
 	}

 	// 輸出統計結果就可以了
 	System.out.println("水仙花數共有:" + count + "個");
 }

}

1.3 while迴圈的格式及基本使用

1.3.1 while迴圈語句格式

基本格式
while(判斷條件語句) {
迴圈體語句;
}

擴充套件格式

初始化語句;

while(判斷條件語句) {
迴圈體語句;
控制條件語句;
}

1.3.2 執行流程圖

程式碼案例

package com.test.day03;

/*
* while迴圈語句的基本格式:
* 		while(判斷條件語句) {
* 			迴圈體語句;
* 		}
* 擴充套件格式:
* 		初始化語句;
* 		while(判斷條件語句) {
* 			迴圈體語句;
* 			控制條件語句;
* 		}
* 
* 回顧for迴圈的語句格式:
* 		for(初始化語句;判斷條件語句;控制條件語句) {
* 			迴圈體語句;
* 		}
*/
public class WhileDemo {
 public static void main(String[] args) {
 	// 輸出10次HelloWorld
 	/*
 	 * for(int x=1; x<=10; x++) { System.out.println("HellloWorld"); }
 	 */

 	// while迴圈實現
 	int x = 1;
 	while (x <= 10) {
 		System.out.println("HellloWorld");
 		x++;
 	}
 }

}

2.4 while迴圈的練習

1)while迴圈實現1-100之間資料求和

示例程式碼,僅供參考

package com.test.day03;

/*
* 求1-100之和。
* 練習:統計水仙花個數。
*/
public class WhileTest {
 public static void main(String[] args) {
 	// 回顧for迴圈實現

 	/*
 	 * //定義求和變數 int sum = 0; //獲取1-100之間的資料 for(int x=1; x<=100; x++) { //累加 sum +=
 	 * x; } System.out.println("1-100的和是:"+sum);
 	 */

 	// while迴圈實現
 	// 定義求和變數
 	int sum = 0;
 	int x = 1;
 	while (x <= 100) {
 		sum += x;
 		x++;
 	}
 	System.out.println("1-100的和是:" + sum);
 }
}

1.5 do…while迴圈的格式及基本使用

1.5.1 do…while迴圈語句格式

基本格式
do {
迴圈體語句;
}while((判斷條件語句);

擴充套件格式
初始化語句;
do {
迴圈體語句;
控制條件語句;
} while((判斷條件語句);

1.5.2 執行流程圖

1.5.3 程式碼案例

package com.test.day03;

/*
* do...while迴圈的基本格式:
* 		do {
* 			迴圈體語句;
* 		}while(判斷條件語句);
* 擴充套件格式:
* 		初始化語句;
* 		do {
* 			迴圈體語句;
* 			控制條件語句;
* 		}while(判斷條件語句);
* 執行流程:
* 		A:執行初始化語句;
* 		B:執行迴圈體語句;
* 		C:執行控制條件語句;
* 		D:執行判斷條件語句,看是true還是false
* 			如果是true,回到B繼續
* 			如果是false,就結束
* 
* 練習:
* 		求和案例
* 		統計水仙花個數
*/
public class DoWhileDemo {
 public static void main(String[] args) {
 	// 輸出10次 HelloWorld
 	/*
 	 * for(int x=1; x<=10; x++) { System.out.println("HelloWorld"); }
 	 */

 	// do...while改寫
 	int x = 1;
 	do {
 		System.out.println("HelloWorld");
 		x++;
 	} while (x <= 10);
 }
}

1.6 三種迴圈的區別

1.6.1 區別概述

雖然可以完成同樣的功能,但是還是有小區別:

1)do…while迴圈至少會執行一次迴圈體。

2)for迴圈和while迴圈只有在條件成立的時候才會去執行迴圈體

3)for迴圈語句和while迴圈語句的小區別:

使用區別:控制條件語句所控制的那個變數,在for迴圈結束後,就不能再被訪問到了,而while迴圈結束還可以繼續使用,如果你想繼續使用,就用while,否則推薦使用for。原因是for迴圈結束,該變數就從記憶體中消失,能夠提高記憶體的使用效率。

程式碼案例

package com.test.day03;

/*
* 三種迴圈的區別:
* 		A:do...while至少執行一次迴圈體
* 		B:for,while迴圈先判斷條件是否成立,然後決定是否執行迴圈體
* 
* for和while的小區別:
* 		for迴圈的初始化變數,在迴圈結束後,不可以被訪問。而while迴圈的初始化變數,是可以被繼續使用的。
* 		如果初始化變數,後面還要繼續訪問,就使用while,否則,推薦使用for。
* 
* 迴圈的使用推薦:
* 		for -- while -- do...while
*/
public class DoWhileDemo {
 public static void main(String[] args) {
 	/*
 	 * int x = 3; while(x<3) { System.out.println("我愛林青霞"); x++; }
 	 * System.out.println("--------------"); int y = 3; do {
 	 * System.out.println("我愛林青霞"); y++; }while(y<3);
 	 */

 	for (int x = 1; x <= 10; x++) {
 		System.out.println("愛生活,愛Java");
 	}
 	// 這裡的x無法繼續訪問
 	// System.out.println(x);
 	System.out.println("-----------------");

 	int y = 1;
 	while (y <= 10) {
 		System.out.println("愛生活,愛Java");
 		y++;
 	}
 	System.out.println(y);
 }
}

關注公眾號'巧嘆',獲取更多知識點和分散式系統專案原始碼及視訊,300多個視訊等你來拿