學習筆記(19):C語言入門到精通-邏輯判斷語句-上

立即學習:https://edu.csdn.net/course/play/10534/378133?utm_source=blogtoedu

學習目標:

1. 掌握if..else的多種使用方法。

2. 掌握switch的使用方法。

掃碼觀看視頻:

代碼示例:

#include <stdio.h>

void switch_select(const int kind) {
    switch (kind) {
        case 1:
        case 2:
            if (kind == 1) {
                printf("《人工智能》\n");
            } else if (kind == 2) {
                printf("《老人與海》\n");
            }

            printf("《時間簡史》\n");
            break;
        case 3:
            printf("《明朝的那些事兒》\n");
            break;
        default:
            printf("什麼都沒有找到\n");
            break;
    }
}

void if_select(const int kind) {
    if (kind == 1) {
        printf("a.《人工智能》\n");
    } else if (kind == 2) {
        printf("b.《老人與海》\n");
    } else if (kind == 3) {
        printf("c.《明朝的那些事兒》\n");
    } else {
        printf("什麼都沒有找到\n");
    }
}

int main() {
    int kind = 0;
    char seq = 0;

    printf("Book Query System\n");
    printf("1. 科技,2. 文學,3. 歷史\n");
    printf("Please select number(1~3):");
    scanf("%d", &kind);

    //switch_select(kind);
    if_select(kind);
    printf("Please input the book seq:");
    scanf("\n%c", &seq);

    switch (seq) {
        case 'a':
            printf("a.《人工智能》\n");
            break;
        case 'b':
            printf("b.《老人與海》\n");
            break;
        case 'c':
            printf("c.《明朝的那些事兒》\n");
            break;
        default:
            printf("Don't found this book.\n");
            break;
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章