# 大數據學習筆記第4天-流程控制 #
## 回顧
## 作用
## 大綱
- Switch結構
- 循環結構
- for循環
- while循環
- do...while循環
- 循環嵌套
- 2個案例:猜數字、循環輸入成績進行判斷
## 第一節課 ##
- 回顧、作業
- Switch結構
- Switch練習
## 第二節課 ##
- for循環
- for循環練習
## 第三節課 ##
- while循環
- do...while循環
- 循環嵌套
## 第四節課 ##
- 案例:循環嵌套之九九乘法表
- 案例:猜數字
- 案例:循環輸入成績進行判斷
## 分支結構:switch
switch(表達式或變量){
case 取值1:
語句體1;
break;
case 取值2:
語句體2;
break;
...
default:
語句體n+1;
break;
}

### switch注意事項:
- switch語句表達式的類型有:byte,short,int,char,String(1.7之后才支持)和枚舉類型;
- case之間與default沒有順序,case相當于入口點,沒有匹配的case將執行default,default也不是必須的;
- 結束switch語句的情況:遇到break或執行到switch語句結束;
- 如果匹配的case或者default沒有對應的break,那么程序會繼續向下執行,運行可以執行的語句,直到遇到break或者switch結尾;
## 循環結構:概述
循環語句可以在滿足循環條件的情況下,反復執行某一段代碼;被反復執行的代碼稱為循環體;需要在適當的時候,把循環條件改為假,從而結束循環,否則循環將一直執行下去,形成死循環;
完整的循環應該包含以下四個部分:
- 初始化語句:初始化工作,在循環體開始之前執行;
- 循環條件:一個boolean表達式,決定是否執行循環體;
- 循環體:反復執行的部分;
- 迭代語句:在循環體執行完之后執行,然后再去判斷循環條件,一般用來控制循環條件中的變量,使循環在合適的時候結束;
## 循環結構:for
格式:
for(初始化表達式;條件表達式;循環變量控制語句){
循環體語句;
}
格式說明:for運行的順序
1. 執行初始化表達式,只執行一次,通常是定義循環變量語句;
2. 判斷循環條件,為真就往下執行,為假就結束循環;
3. 執行循環體語句;
4. 執行循環變量控制語句,一般是循環變量的自增或自減;
5. 回到第2步,重復這個過程,直到為假時結束;
## 10個練習題
1. 在控制臺上依次打印1-10;
2. 在控制臺上依次打印10-1;
3. 求1-10所有數之和;
4. 求出1-100之間的所有偶數之和;
5. 求出1-100之間的所有奇數之和;
6. 求出5的階乘;
7. 求水仙花數;
8. 列出5位數中的回文數;
9. 統計1000以內,同時是3,5,7的倍數的數有多少個?
10. 在1-100之間,若是3的倍數,則在它之后輸出中文“三”,若是5的倍數,則在它之后輸出中文“五”,若是7的倍數,則在它之后輸出中文“七”
答案:
01題:
public class Demo01{
public static void main(String[] args){
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}
02題:
public class Demo02{
public static void main(String[] args){
for(int i=10;i>0;i--){
System.out.println(i);
}
}
}
03題:
public class Demo03{
public static void main(String[] args){
int sum = 0;
for(int i=1;i<=10;i++){
sum += i;
}
System.out.println("求1-10所有數之和:" + sum);
}
}
04題:
public class Demo04{
public static void main(String[] args){
int sum = 0;
for(int i=1; i<=100; i++){
if(i%2==0){
sum += i;
}
}
System.out.println("求出1-100之間的所有偶數之和:" + sum);
}
}
05題:
public class Demo05{
public static void main(String[] args){
int sum = 0;
for(int i=1; i<=100; i++){
if(i%2==1){
sum += i;
}
}
System.out.println("求出1-100之間的所有奇數之和:"+sum); //2500
}
}
06題:
/*
求出5的階乘;
公式:n!=n*(n-1)! 階乘的計算方法 階乘指從1乘以2乘以3乘以4一直乘到所要求的數。
*/
public class Demo06{
public static void main(String[] args){
int res = 1;
for(int i=1; i<=5; i++){
res = res * i;
}
System.out.println("5的階乘:" + res);
}
}
07題:
/*
求水仙花數;
所謂“水仙花數”是指一個三位數,其各位數字立方和等于該數 本身。
*/
public class Demo07{
public static void main(String[] args){
for(int i=100; i<1000; i++){
int ge = i%10; // 個位數
int shi = i/10%10; // 十位數
int bai = i/100; // 百位數
if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i){
System.out.println(i);
}
}
}
}
08題:
/*
列出5位數中的回文數
回文數的概念:即是給定一個數,這個數順讀和逆讀都是一樣的。例如:121,1221...
*/
public class Demo08{
public static void main(String[] args){
int count = 0;
for(int i=10000; i<100000; i++){
// 個十百千萬
int ge = i%10; // 12345
int shi = i%100/10;
int qian = i%10000/1000;
int wan = i/10000;
if(ge==wan && shi==qian){
count++;
System.out.println(i);
}
}
System.out.println("5位數中一共有"+count+"個回文數"); // 900
}
}
09題:
/*
統計1000以內,同時是3,5,7的倍數的數有多少個?
*/
public class Demo09{
public static void main(String[] args){
int count = 0;
for (int i=0; i<1000; i++){
if(i%3==0 && i%5==0 && i%7==0){
System.out.println(i);
count++;
}
}
System.out.println("統計1000以內,同時是3,5,7的倍數的數有多少個?" + count);
}
}
10題:
/*
在1-100之間,若是3的倍數,則在它之后輸出中文“三”,若是5的倍數,則在它之后輸出中文“五”,若是7的倍數,則在它之后輸出中文“七”
*/
public class Demo10{
public static void main(String[] args){
for(int i=1; i<=100; i++){
System.out.print(i);
if(i%3==0){
System.out.print("三");
}else if(i%5==0){
System.out.print("五");
}else if(i%7==0){
System.out.print("七");
}
System.out.println();
}
}
}
## 循環結構:while
while語句格式:
[初始化部分]
while(條件表達式){
循環體語句;
[循環變量控制語句]
}
執行流程:
1. 執行初始化語句(如果有的話);
2. 判斷條件表達式真假;如果真,往下執行;如果假,結束循環;
3. 執行循環體語句;
4. 執行循環變量控制語句(如果有的話);
5. 返回第2步,重復執行,直到條件為假,結束循環;
### 循環變量的使用問題:
for循環的循環變量,一般情況下在循環外是不能訪問到的,因為它的作用域是在for循環的{}之內,但是可以通過把循環變量定義在循環之外來實現這個功能;或者定義計數器;
for(int i=0;i<10;i++){
...
}
System.out.println(i); //error
int i=0;
for(;i<10;i++){
...
}
System.out.println(i); //ok
while循環本身就是把循環變量定義在循環之外,所以沒有這個問題;
int i=0;
while(i<10){
...
i++;
}
System.out.println(i);
### for 和 while 的使用場景
1. 當明確知道循環的范圍的時候,通常使用for;
2. 當不明確循環次數的時候,for和while都可以,通常使用while;
### 案例:
已知珠穆朗瑪峰的高度是8848米,假設有一張足夠大的紙,厚度是0.01米,請問:這張紙折疊多少次可以保證厚度不低于珠穆朗瑪峰的高度?
答案:
public class ZhuMuLangMa2{
public static void main(String[] args){
double h=0.01;
double all_height = h;
int count=0;
while(all_height<=8848){
all_height *= 2;
count++;
}
System.out.println("需要折疊的次數是:"+count);
}
}
另外一種方法:
public class ZhuMuLangMa{
public static void main(String[] args){
double h=0.01;
double all_height = h;
int count=0;
while(true){
all_height *= 2;
count++;
if(all_height>8848){
break;
}
}
System.out.println("需要折疊的次數是:"+count);
}
}
## 循環結構:do-while
do while語句格式:
[初始化部分]
do{
循環體語句;
[循環變量控制語句]
}while(條件表達式);

執行流程:
1. 執行初始化部分(如果有)
2. 執行循環體語句
3. 執行循環變量控制語句(如果有)
4. 判斷條件表達式,如果為真,返回第2步;如果為假,結束循環
特點:循環體至少被執行1次;
### 三種循環的比較和死循環
- while和for可以互換;
- do-while循環的循環體語句至少執行一次;
- 優先選擇使用for循環;
- 死循環:條件表達式永遠為true;
## 循環的嵌套
外層循環表達式為真時,進入內層循環條件的判斷,內層循環結束時,跳到外層循環繼續判斷外層循環的條件;

代碼實例:
public class QianTao{
public static void main(String[] args){
for(int i=0; i<5; i++){ //外層循環
for(int j=0; j<5; j++){ //內層循環
System.out.println(i+"*"+j);
}
System.out.println("-------");
}
}
}

### 案例
循環嵌套打印正三角形、倒三角形;
代碼:
/*
打印三角形:第n行打印n次星號
*/
import java.util.Scanner;
public class SanJiaoXing{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("請輸入一個數字,表示三角形的行數:");
int num = s.nextInt();
for(int i=1; i<=num; i++){
for(int j=i; j>0; j--){
System.out.print("*");
}
System.out.println();
}
}
}

備注:可以試試輸出一個倒三角形。