Java 條件控制語法分為下列幾類
參考 p.4-2 範例程式 IfElse.java
import java.util.Scanner;
public class E01_IfElse {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入成績:");
int score = sc.nextInt(); // 平均成績
if (score >= 85) { // if區塊內程式碼只有一行,可以省略大括號
System.out.println("獲得獎學金10000元!");
} else { // else區塊內程式碼只有一行,也可省略大括號
System.out.println("還要再努力!");
}
}
}
參考 p.4-3 範例程式 IfElsePlus.java
import java.util.Scanner;
public class E02_IfElsePlus {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入成績:");
int score = sc.nextInt(); // 平均成績
if (score >= 85) // if區塊內程式碼只有一行,省略大括號,但是不建議
System.out.println("獲得獎學金10000元!");
else if (score >= 75) // else區塊內程式碼只有一行,省略大括號,但是不建議
System.out.println("獲得獎學金5000元!");
else
// else區塊內程式碼只有一行,省略大括號,但是不建議
System.out.println("還要再努力!");
}
}
參考 p.4-4 程式 IfElseNested.java
public class E03_IfElseNested {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入成績:");
int score = sc.nextInt();
if (score >= 75) {
if (score >= 85) {
System.out.println("獲得獎學金10000元!");
} else {
System.out.println("獲得獎學金5000元!");
}
} else {
System.out.println("還要再努力!");
}
}
}
參考 p.4-5 範例程式 If.java
public class E04_If {
public static void main(String[] args) {
boolean isDizzy = true; // 是否頭暈
boolean isVomit = true; // 是否嘔吐
// 兩者的發生與否,為獨立事件,需要使用2個IF來判斷
if (isDizzy)
System.out.println("服用緩和頭暈藥");
if (isVomit)
System.out.println("服用緩和嘔吐藥");
}
}
條件運算符號(?:)與if-else類似,參考下列範例
參考 p.4-5 程式 ConditionOP.java
public class E05_ConditionOP {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入成績:");
int score = sc.nextInt(); // 平均成績
String s1 = "獲得獎學金10000元!";
String s2 = "還要再努力!";
String r = score >= 85 ? s1 : s2;
System.out.println(r);
}
}
參考 p.4-7 範例程式 SwitchCase.java
import java.util.Scanner;
public class E06_SwitchCase {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入名次:");
int rank = sc.nextInt();
switch (rank) {
case 1:
System.out.println("第1名,獎學金10000元!");
break;
case 2:
System.out.println("第2名,獎學金5000元!");
break;
case 3:
System.out.println("第3名,獎學金2000元!");
break;
default:
System.out.println("還要再努力!");
break;
}
}
}
參考 p.4-8 範例程式 SwitchCaseBreak.java
import java.util.Scanner;
public class E07_SwitchCaseBreak {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入名次:");
int rank = sc.nextInt();
switch (rank) {
case 1:
System.out.println("第1名,獎學金10000元!");
case 2:
System.out.println("第2名,獎學金5000元!");
case 3:
System.out.println("第3名,獎學金2000元!");
break;
default:
System.out.println("還要再努力!");
break;
}
}
}
迴圈(loop):在達到某個目的前,不斷、重複地做同樣的是,直到達成這個目的為止
Java 主要有3種迴圈控制
while(條件式) { // 當條件式為true時,執行迴圈內程式
// 迴圈內程式
}
參考 p.4-10 範例程式 While.java
public class E08_While {
public static void main(String[] args) {
int count = 1;
while (count <= 10) {
System.out.print(count + " ");
count++; // 移除此行會陷入無限迴圈
}
System.out.println("列印完畢");
}
}
do { // 先執行迴圈內程式,然後再判斷條件式
// 迴圈內程式 (至少被執行1次)
} while(條件式) // 當條件式為true時,繼續執行迴圈內程式
參考 p.4-11 範例程式 DoWhile.java
public class E09_DoWhile {
public static void main(String[] args) {
int count = 11;
do {
System.out.printf("count=%d\n", count);
count++; // 移除此行會陷入無限迴圈
} while (count <= 10);
System.out.println("列印完畢");
}
}
for(int count=1; count<=10; count++){
// 迴圈內程式
}
以分號;將for迴圈分成3部分
參考 p.4-12 範例程式 For.java
public class E10_For {
public static void main(String[] args) {
for (int count = 1; count <= 10; count++) {
System.out.print(count + " ");
}
System.out.println("列印完畢");
}
}
for (int cnt1 = 0, cnt2 = 0; cnt1 + cnt2 <= 150; cnt1 += 20, cnt2 += 30) {
// 迴圈內程式
}
參考 p.4-13 範例程式 ForFactors.java
public class E11_ForFactors {
public static void main(String[] args) {
int day = 0; // 天數
// tangerine代表橘子數量,orange代表柳丁數量
for (int tangerine = 0, orange = 0; tangerine + orange <= 150; tangerine += 20, orange += 30) {
System.out.print("第" + day + "天\t");
System.out.print("橘子數量:" + tangerine + "\t");
System.out.print("柳丁數量:" + orange + "\t");
System.out.println("總數量:" + (tangerine + orange));
// 或將上面4個print,合併為下列printf
System.out.printf("第%d天\t橘子數量:%d\t柳丁數量:%d\t總數量:%d\n", day,
tangerine, orange, tangerine + orange);
day++;
}
System.out.println("裝滿一箱水果!");
}
}
參考 p.4-13 範例程式 ForNested.java
public class E12_ForNested {
public static void main(String[] args) {
for (int m = 1; m <= 5; m++) {
System.out.printf("%2d: ", m);
for (int n = 1; n <= 10; n++)
System.out.printf("%4d ", m + n);
System.out.println();
}
}
}
1: 1 2 3 4 5 6 7 8 9 2: 2 4 6 8 10 12 14 16 18 3: 3 6 9 12 15 18 21 24 27 4: 4 8 12 16 20 24 28 32 36 5: 5 10 15 20 25 30 35 40 45 6: 6 12 18 24 30 36 42 48 54 7: 7 14 21 28 35 42 49 56 63 8: 8 16 24 32 40 48 56 64 72 9: 9 18 27 36 45 54 63 72 81
1x1= 1 1x2= 2 1x3= 3 1x4= 4 1x5= 5 1x6= 6 1x7= 7 1x8= 8 1x9= 9 2x1= 2 2x2= 4 2x3= 6 2x4= 8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18 3x1= 3 3x2= 6 3x3= 9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27 4x1= 4 4x2= 8 4x3=12 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 5x1= 5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45 6x1= 6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 6x7=42 6x8=48 6x9=54 7x1= 7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 7x8=56 7x9=63 8x1= 8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 8x9=72 9x1= 9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
參考 p.4-16 範例程式 LoopBreak.java
public class E13_LoopBreak {
public static void main(String[] args) {
int sick = 5; // 第5堂課請假
for (int num = 1; num <= 8; num++) {
if (num == sick) {
System.out.println("身體不適,回家休息!");
break;
}
System.out.println("上完第" + num + "堂課");
}
}
}
參考 p.4-17 範例程式 LoopContinue.java
public class E14_LoopContinue {
public static void main(String[] args) {
int absent = 5; // 第5堂課翹課
for (int num = 1; num <= 8; num++) {
if (num == absent) {
System.out.println("蹺課出去玩!");
continue;
}
System.out.println("上完第" + num + "堂課");
}
}
}
參考 p.4-18 範例程式 LoopLabel.java
public class E15_LoopLabel {
public static void main(String[] args) {
int faint = 3; // 跑第3圈昏倒了
labA: // 標籤與迴圈之間不可以插入其他程式碼
for (int cls = 1; cls <= 8; cls++) {
if (cls == 6) {
for (int run = 0; run <= 5; run++) {
if (run == faint) {
System.out.println("<<昏倒了,送急診>>");
break labA;
}
System.out.print("跑完" + run + "圈 ");
}
}
System.out.println("上完第" + cls + "堂課");
}
}
}