#include <stdio.h> int main() { int a,b,c,max; printf("Please enter three integers: "); scanf("%d%d%d",&a,&b,&c); if (a >= b && a >= c) { max = a; }else if (b >= a && b >= c) { max = b; } else { max = c; } printf("The maximum ids %d.", max); return 0; }
switch 述句
switch (整数值) { case 常数整数值: 程式片段; break; default: 程式片段; break; }
如:
#include <stdio.h> int main() { int id; printf("ID: "); scanf("%d",&id); switch (id) { case 2: printf("John "); break; case 13: printf("Mary "); break; default: printf("Not found "); break; } return 0; }
练习:消费金额计算的练习(使用switch述句)
#include <stdio.h> int main() { int total = 0; int id; do{ scanf("%d", &id); switch (id) { case 1: total += 90; break; case 2: total += 75; break; case 3: total += 83; break; case 4: total += 89; break; case 5: total += 71; break; } }while (id != 0); printf("Total: %d ", total); return 0; }