目錄
1. 順序結構
2. 分支結構
2.1 if語句
2.1.1 語法格式1
2.1.2 語法格式2
2.1.3 語法格式3 ?
2.1.4?練習
2.1.5 注意事項
2.2?switch 語句
3. 循環結構
3.1 while循環
3.1.1 語法格式
3.1.2?代碼示例
3.1.3?注意事項
3.2 break
3.3 continue
3.4 for循環
3.4.1 語法格式
3.4.2?代碼示例
3.4.3?注意事項?(和while循環類似)
3.5?do while 循環
3.5.1?語法格式
3.5.2 代碼示例
3.5.3 注意事項
4.?輸入輸出
4.1 輸出到控制臺
4.1.1 基本語法
4.1.2 代碼示例
4.1.3 格式化字符串
4.2 從鍵盤輸入
5. 猜數字游戲
1. 順序結構
順序結構比較簡單,按照代碼書寫的順序一行一行執行。

如果調整代碼的書寫順序, 則執行順序也發生變化
2. 分支結構
2.1 if語句
2.1.1 語法格式1
如果布爾表達式結果為true,執行if中的語句,否則不執行。
比如:小明,如果這次考試考到90分或以上,給你獎勵一個冰淇淋。
int score = 92;
if(score >= 90){System.out.println("吃個冰淇淋!!!");
}
2.1.2 語法格式2
如果布爾表達式結果為true,則執行if中語句,否則執行else中語句。
比如:小明,如果這次考到90分以上,給你獎勵一個冰淇淋,否則不能吃。
int score = 92;
if(score >= 90){System.out.println("吃個冰淇淋!!!");
}else{System.out.println("不能吃!!!");
}
2.1.3 語法格式3 ?
表達式1成立,執行語句1,否則表達式2成立,執行語句2,否則執行語句3
比如:考慮到學生自尊,不公開分數排名,因此:
- 分數在 [90, 100] 之間的,為優秀
- 分數在 [80, 90) 之前的,為良好
- 分數在 [70, 80) 之間的,為中等
- 分數在 [60, 70) 之間的,為及格
- 分數在 [ 0, 60) 之間的,為不及格
- 錯誤數據
按照上述辦法通知學生成績。
if(score >= 90){System.out.println("優秀");
}else if(score >= 80 && score < 90){System.out.println("良好");
}else if(score >= 70 && score < 80){System.out.println("中等");
}else if(score >= 60 && score < 70){System.out.println("及格");
}else if(score >= 0 && score < 60){System.out.println("不及格");
}else{System.out.println("錯誤數據");
}
2.1.4?練習
1. 判斷一個數字是奇數還是偶數
int num = 10;
if (num % 2 == 0) {System.out.println("num 是偶數");
} else {System.out.println("num 是奇數");
}
2.?判斷一個數字是正數,負數,還是零
int num = 10;
if (num > 0) {
System.out.println("正數");
} else if (num < 0) {
System.out.println("負數");
} else {
System.out.println("0");
}
3. 判斷一個年份是否為閏年
int year = 2000;
if (year % 100 == 0) {
// 判定世紀閏年if (year % 400 == 0) {System.out.println("是閏年");} else {System.out.println("不是閏年");}
} else {
// 普通閏年if (year % 4 == 0) {System.out.println("是閏年");} else {System.out.println("不是閏年");}
}
2.1.5 注意事項
- 代碼風格
雖然兩種方式都是合法的, 但是 Java 中更推薦使用風格1, { 放在 if / else 同一行,代碼更緊湊。
- 分號問題
此處多寫了一個 分號,導致分號成為了 if 語句的語句體, 而 { } 中的代碼已經成為了和一個 if 無關的代碼塊。
- 懸垂 else 問題
if / else 語句中可以不加 大括號,但是也可以寫語句(只能寫一條語句),?此時 else 是和最接近的 if 匹配,但是實際開發中我們 不建議 這么寫,最好加上大括號。
2.2?switch 語句
基本語法
執行流程:
1. 先計算表達式的值
2. 和case依次比較,一旦有響應的匹配就執行該項下的語句,直到遇到break時結束
3. 當表達式的值沒有與所列項匹配時,執行default
int day = 1;
switch(day) {case 1:System.out.println("星期一");break;case 2:System.out.println("星期二");break;case 3:System.out.println("星期三");break;case 4:System.out.println("星期四");break;case 5:System.out.println("星期五");break;case 6:System.out.println("星期六");break;case 7:System.out.println("星期日");break;default:System.out.println("輸入有誤");break;
}
【注意事項】
1. 多個case后的常量值不可以重復
2. switch的括號內只能是以下類型的表達式:
????????基本類型:byte、char、short、int,注意不能是long類型
????????引用類型:String常量串、枚舉類型
????????
3.?break 不要遺漏, 否則會失去 "多分支選擇" 的效果
4. switch 不能表達復雜的條件
5. switch 雖然支持嵌套, 但是很丑,一般不推薦~
代碼的美觀程度也是一個重要的標準. 畢竟這是看臉的世界。
綜上, 我們發現, switch 的使用局限性是比較大的。
3. 循環結構
3.1 while循環
3.1.1 語法格式
循環條件為 true, 則執行循環語句; 否則結束循環。
3.1.2?代碼示例
代碼示例1:打印 1 - 10 的數字
int num = 1;
while (num <= 10) {System.out.println(num);num++;
}
代碼示例2:?計算 1 - 100 的和
int n = 1;
int result = 0;
while (n <= 100) {result += n;n++;
}
System.out.println(num);// 執行結果
5050
代碼示例3:計算 5 的階乘
int n = 1;
int result = 1;
while (n <= 5) {
result *= n;
n++;
}
System.out.println(num);// 執行結果
120
代碼示例4:計算 1! + 2! + 3! + 4! + 5!
int num = 1;
int sum = 0;
// 外層循環負責求階乘的和
while (num <= 5) {int factorResult = 1;int tmp = 1;// 里層循環負責完成求階乘的細節.while (tmp <= num) {factorResult *= tmp;tmp++;}sum += factorResult;num++;
}
System.out.println("sum = " + sum);
這里我們發現,當一個代碼中帶有多重循環的時候,?代碼的復雜程度就大大提高了,而比較復雜的代碼就更容易出錯。后面我們會采用更簡單的辦法來解決這個問題。
3.1.3?注意事項
1. 和 if 類似,while 下面的語句可以不寫 { } , 但是不寫的時候只能支持一條語句. 建議還是加上 { }?
2. 和 if 類似,while 后面的 { 建議和 while 寫在同一行
3. 和 if 類似,while 后面不要多寫 分號, 否則可能導致循環不能正確執行
int num = 1;
while (num <= 10); {System.out.println(num);num++;
}// 執行結果
[無任何輸出, 程序死循環]
此時 ;為 while 的語句體(這是一個空語句),,實際的 { } 部分和循環無關。?此時循環條件 num <= 10 恒成立,致代碼死循環了。
3.2 break
break 的功能是讓循環提前結束。
代碼示例: 找到 100 - 200 中第一個 3 的倍數
int num = 100;
while (num <= 200) {if (num % 3 == 0) {System.out.println("找到了 3 的倍數, 為:" + num);break;}num++;
}// 執行結果
找到了 3 的倍數, 為:102
執行到 break 就會讓循環結束。
3.3 continue
continue 的功能是跳過這次循環, 立即進入下次循環。
代碼示例: 找到 100 - 200 中所有 3 的倍數
int num = 100;
while (num <= 200) {if (num % 3 != 0) {num++; // 這里的 ++ 不要忘記! 否則會死循環.continue;}System.out.println("找到了 3 的倍數, 為:" + num);num++;
}
執行到 continue 語句的時候, 就會立刻進入下次循環(判定循環條件), 從而不會執行到下方的打印語句。
3.4 for循環
3.4.1 語法格式
- 表達式1: 用于初始化循環變量初始值設置,在循環最開始時執行,且只執行一次
- 表達式2: 循環條件,滿則循環繼續,否則循環結束
- 表達式3: 循環變量更新方式
【執行過程】
①②③④--->②③④--->②③④--->②③④--->②③④--->②③④--->...--->②為false,循環結束。
3.4.2?代碼示例
代碼示例1:打印 1 - 10 的數字
for (int i = 1; i <= 10; i++) {System.out.println(i);
}
代碼示例2:計算 1 - 100 的和
int sum = 0;
for (int i = 1; i <= 100; i++) {sum += i;
}
System.out.println("sum = " + sum);// 執行結果
5050
代碼示例3:計算 5 的階乘
int result = 1;
for (int i = 1; i <= 5; i++) {result *= i;
}
System.out.println("result = " + result);
int sum = 0;
for (int i = 1; i <= 5; i++) {int tmp = 1;for (int j = 1; j <= i; j++) {tmp *= j;}sum += tmp;
}
System.out.println("sum = " + sum);
3.4.3?注意事項?(和while循環類似)
1. 和 if 類似, for 下面的語句可以不寫 { },但是不寫的時候只能支持一條語句。建議還是加上 { }
2. 和 if 類似。for 后面的 { 建議和 while 寫在同一行
3. 和 if 類似,for 后面不要多寫 分號, 否則可能導致循環不能正確執行
4. 和while循環一樣,結束單趟循環用continue,結束整個循環用break
3.5?do while 循環
3.5.1?語法格式
先執行循環語句, 再判定循環條件,循環條件成立則繼續執行,否則循環結束。
3.5.2 代碼示例
打印 1 - 10
int num = 1;
do {System.out.println(num);num++;
} while (num <= 10);
3.5.3 注意事項
1. do while 循環最后的分號不要忘記
2. 一般 do while 很少用到, 更推薦使用 for 和 while
4.?輸入輸出
4.1 輸出到控制臺
4.1.1 基本語法
println 輸出的內容自帶 \n,?print 不帶 \n
printf 的格式化輸出方式和 C 語言的 printf 是基本一致的
4.1.2 代碼示例
System.out.println("hello world");
int x = 10;
System.out.printf("x = %d\n", x)
4.1.3 格式化字符串
這個表格沒必要記住, 用到的時候根據需要查一下就行了。
4.2 從鍵盤輸入
使用 Scanner 讀取字符串/整數/浮點數
import java.util.Scanner; // 需要導入 util 包Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的姓名:");
String name = sc.nextLine();
System.out.println("請輸入你的年齡:");
int age = sc.nextInt();
System.out.println("請輸入你的工資:");
float salary = sc.nextFloat();
System.out.println("你的信息如下:");
System.out.println("姓名: "+name+"\n"+"年齡:"+age+"\n"+"工資:"+salary);
sc.close(); // 注意, 要記得調用關閉方法// 執行結果
請輸入你的姓名:
張三
請輸入你的年齡:
18
請輸入你的工資:
1000
你的信息如下:
姓名: 張三
年齡:18
工資:1000.0
使用 Scanner 循環讀取 N 個數字,并求取其平均值
Scanner sc = new Scanner(System.in);
int sum = 0;
int num = 0;
while (sc.hasNextInt()) {int tmp = sc.nextInt();sum += tmp;num++;
}
System.out.println("sum = " + sum);
System.out.println("avg = " + sum / num);
sc.close();// 執行結果
10
40.0
50.5
^Z
sum = 150.5
avg = 30.1
注意事項:當循環輸入多個數據的時候, 使用 ctrl + z 來結束輸入 (Windows 上使用 ctrl + z, Linux / Mac 上使用 ctrl + d)
5. 猜數字游戲
5.1 游戲規則
系統自動生成一個隨機整數(1-100), 然后由用戶輸入一個猜測的數字,?如果輸入的數字比該隨機數小,提示 "低了",如果輸入的數字比該隨機數大, 提示 "高了",如果輸入的數字和隨機數相等,則提示 "猜對了"。
5.2 參考代碼
import java.util.Random;
import java.util.Scanner;class Test {public static void main(String[] args) {Random random = new Random(); // 默認隨機種子是系統時間Scanner sc = new Scanner(System.in);int toGuess = random.nextInt(100);// System.out.println("toGuess: " + toGuess);while (true) {System.out.println("請輸入要輸入的數字: (1-100)");int num = sc.nextInt();if (num < toGuess) {System.out.println("低了");} else if (num > toGuess) {System.out.println("高了");} else {System.out.println("猜對了");break;}}sc.close();}
}