使用Switch语句实现分数等级判定

#include <stdio.h>

int main() {
    int score;
    printf("请输入分数:");
    scanf("%d", &score);
    switch (score / 10) {
        case 10:
        case 9:
            printf('等级为A\n');
            break;
        case 8:
            printf('等级为B\n');
            break;
        case 7:
            printf('等级为C\n');
            break;
        case 6:
            printf('等级为D\n');
            break;
        default:
            printf('等级为E\n');
            break;
    }
    return 0;
}

使用If语句实现分数等级判定

#include <stdio.h>

int main() {
    int score;
    printf("请输入分数:");
    scanf("%d", &score);
    if (score >= 90) {
        printf('等级为A\n');
    } else if (score >= 80) {
        printf('等级为B\n');
    } else if (score >= 70) {
        printf('等级为C\n');
    } else if (score >= 60) {
        printf('等级为D\n');
    } else {
        printf('等级为E\n');
    }
    return 0;
}

本文展示了两种使用C语言实现分数等级判定的方法,分别是使用Switch语句和If语句。Switch语句适用于处理有限个离散值的情况,而If语句则适用于处理连续的数值范围。您可以根据实际情况选择合适的结构来编写您的代码。


原文地址: https://www.cveoy.top/t/topic/kEhz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录