3.4 switch 语句

记住一些编程建议 or 教导:

3.4.1 choise-your-num.c
#include <stdio.h>

int main() {
    int choice;
    printf("请选择一个数字(1-3):");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("您选择了数字 1。\n");
            break;
        case 2:
            printf("您选择了数字 2。\n");
            break;
        case 3:
            printf("您选择了数字 3。\n");
            break;
        default:
            printf("无效的选择。\n");
            break;
    }

    return 0;
}

请选择一个数字(1-3):1
您选择了数字 1。

Saving session...completed.

注意:

  • 各分支表达式必须互不相同

  • default 分支是可选的

  • 无匹配,switch 语句不执行什么动作

Last updated