判斷語句是程序邏輯的分水嶺。本文精選 8 個涵蓋生活、商業、行政場景的經典案例,系統演示 if-else 多重分支與 switch-case 多分支的實戰應用、邊界處理、輸入驗證與輸出格式化技巧。
int year, month;
scanf("%d%d", &year, &month);
if (month < 1 || month > 12) printf("月份錯誤\n");
else if (year < 0) printf("年份錯誤\n");
else if (month == 2) {
if ((year%4==0 && year%100!=0) || year%400==0) printf("%d年%d月29天\n", year, month);
else printf("%d年%d月28天\n", year, month);
}
else if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
printf("%d年%d月31天\n", year, month);
else printf("%d年%d月30天\n", year, month);
switch (month) {
case 4: case 6: case 9: case 11: printf("30天\n"); break;
case 2:
if ((year%4==0 && year%100!=0) || year%400==0) printf("29天\n");
else printf("28天\n");
break;
default: printf("31天\n"); // 1,3,5,7,8,10,12
}
int month, cabin; const double base = 1200;
scanf("%d%d", &month, &cabin);
if (month >= 9 && month <= 12) { // 旺季
if (cabin == 1) printf("頭等艙9.5折: %.2f\n", base*0.95);
else if (cabin == 2) printf("經濟艙8.5折: %.2f\n", base*0.85);
} else { // 淡季
if (cabin == 1) printf("頭等艙5折: %.2f\n", base*0.5);
else if (cabin == 2) printf("經濟艙2折: %.2f\n", base*0.2);
}
double a, b, result; char op;
scanf("%lf %c %lf", &a, &op, &b);
switch (op) {
case '+': result = a+b; break;
case '-': result = a-b; break;
case '*': result = a*b; break;
case '/':
if (b == 0) { printf("除數不能為0\n"); return 0; }
result = a/b; break;
default: printf("運算符錯誤\n"); return 0;
}
printf("%.2f %c %.2f = %.2f\n", a, op, b, result);
int weekday, tail;
scanf("%d%d", &weekday, &tail);
int restricted = 0;
switch (weekday) {
case 1: restricted = (tail==2 || tail==7); break;
case 2: restricted = (tail==3 || tail==8); break;
case 3: restricted = (tail==4 || tail==9); break;
case 4: restricted = (tail==5 || tail==0); break;
case 5: case 6: case 7: restricted = 0; break; // 假設周末不限行
}
if (restricted) printf("今天限行,尾號%d不能上路\n", tail);
else printf("今天不限行\n");
char c1, c2;
scanf(" %c %c", &c1, &c2); // 空格跳過換行
if (c1 > c2) printf("較大: %c\n", c1);
else if (c1 < c2) printf("較大: %c\n", c2);
else printf("相等\n");
double a, b, c, max;
scanf("%lf%lf%lf", &a, &b, &c);
max = a;
if (b > max) max = b;
if (c > max) max = c;
printf("最大值: %.4f\n", max);
long amount;
scanf("%ld", &amount);
long denominations[] = {100, 50, 20, 10, 5, 1};
int counts[6] = {0};
for (int i = 0; i < 6; i++) {
counts[i] = amount / denominations[i];
amount %= denominations[i];
}
printf("面額: 100 50 20 10 5 1\n");
printf("張數: ");
for (int i = 0; i < 6; i++) printf("%-5d", counts[i]);
printf("\n");
double weight, fee;
scanf("%lf", &weight);
if (weight <= 0) printf("重量錯誤\n");
else if (weight <= 50) fee = weight * 0.15;
else fee = 50*0.15 + (weight-50)*0.10;
printf("托運費: %.2f 元\n", fee);
double x, y;
scanf("%lf", &x);
if (x < 1) y = x;
else if (x < 10) y = 2*x - 1;
else y = 3*x - 11;
printf("y = %.3f\n", y);
double salary, taxable, tax = 0;
scanf("%lf", &salary);
taxable = salary - 300 - 3500; // 扣除費用+起征點
if (taxable > 0) {
if (taxable <= 1500) tax = taxable * 0.03;
else if (taxable <= 4500) tax = taxable * 0.10 - 105;
else if (taxable <= 9000) tax = taxable * 0.20 - 555;
else if (taxable <= 35000) tax = taxable * 0.25 - 1005;
else if (taxable <= 55000) tax = taxable * 0.30 - 2755;
else if (taxable <= 80000) tax = taxable * 0.35 - 5505;
else tax = taxable * 0.45 - 13505;
printf("個稅: %.2f\n", tax);
} else printf("免稅\n");
double profit, bonus;
scanf("%lf", &profit);
if (profit <= 10) bonus = profit * 0.10;
else if (profit <= 20) bonus = 1.0 + (profit-10)*0.075;
else if (profit <= 40) bonus = 1.0 + 0.75 + (profit-20)*0.05;
else if (profit <= 60) bonus = 1.0 + 0.75 + 1.0 + (profit-40)*0.03;
else if (profit <= 100) bonus = 1.0 + 0.75 + 1.0 + 0.6 + (profit-60)*0.015;
else bonus = 1.0 + 0.75 + 1.0 + 0.6 + 0.6 + (profit-100)*0.01;
printf("提成: %.4f 萬\n", bonus);
double principal; char type; int kind;
scanf("%lf %c", &principal, &type);
if (type=='h'||type=='H') printf("活期: %.2f\n", principal*0.0035);
else if (type=='d'||type=='D') {
scanf("%d", &kind);
switch (kind) {
case 1: printf("整存整取: %.2f\n", principal*0.0325); break;
case 2: printf("零存整取: %.2f\n", principal*0.0285); break;
case 3: printf("定活兩便: %.2f\n", principal*0.0325*0.6); break;
default: printf("定期類型錯誤\n");
}
} else printf("類型錯誤\n");
double years; int edu, qual, score = 0;
scanf("%lf%d%d", &years, &edu, &qual);
if (years == 0) score += 0;
else if (years < 1) score += 1;
else if (years < 3) score += 2;
else if (years < 7) score += 3;
else score += 4;
switch (edu) { case 1: score+=1; break; case 2: score+=2; break; case 3: score+=3; break; }
switch (qual) { case 1: score+=0; break; case 2: score+=1; break; case 3: score+=2; break; case 4: score+=3; break; }
printf("總分: %d\n", score);
scanf(" %c", &c) 空格跳過空白;數值輸入檢查返回值。const 或宏,避免魔術數字。case 1: case 2: ... 合併同邏輯分支。%.2f 保留兩位小數,%-5d 左對齊對齊表格。這 13 個案例覆蓋了條件判斷、多分支選擇、分段函數、累進計算、輸入驗證、格式化輸出等核心技能,反覆練習並嘗試修改邊界條件、擴展功能,即可建立堅實的判斷邏輯編程直覺。