Notes on C language brushing questions - judging grades

insert image description here

topic

Use the nesting of conditional operators to complete this question: students with grades >=90 are represented by A, those with scores between 60 and 89 are represented by B, and those with scores below 60 are represented by C.
insert image description here

ideas

Use if or switch statement to conditionally judge the score, and then output the result according to the judgment.

answer

spelling one

#include <stdio.h>

int main()
{
    
    
    int score;
    char grade;

    printf("请输入您的分数:\n");

    scanf("%d",&score);

    grade=score>=90?'A':(score>=60?'B':'C');

    printf("等级:%c",grade);

    return 0;
}

spelling two

#include <stdio.h>

int main()
{
    
    
    int score;

    printf("请输入您的分数:\n");

    scanf("%d",&score);

    switch(score/10)
    {
    
    
        case 10:
        case 9:
            printf("等级:A");
            break;
        case 8:
        case 7:
        case 6:
            printf("等级:B");
            break;
        case 5:
        case 4:
        case 3:
        case 2:
        case 1:
        case 0:
            printf("等级:C");
            break;
        default:
            printf("输入错误!");

    }

    return 0;
}

Sample output

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/qq_21484461/article/details/124169946