0
Follow
2
View

C language programming a score corresponding to the grade code problem

cxs_2011 注册会员
2023-02-28 20:23

This answer quotes ChatGPT

Your code logic is correct, but you did not print "The level of your score is :" when you entered incorrectly, so you need to add this line to the branch where you entered incorrectly. In addition, you can separate the "less than 0" and" greater than 100" criteria for incorrect input to improve code readability. Here is the modified code:


#include 
#include 
 
int main(void)
{
    char level;
    float score;
    printf("输入你的分数:");
    scanf("%f",&score);
 
    if(score < 0.0)
    {
        printf("输入有误,分数不能小于0");
    }
    else if(score > 100.0)
    {
        printf("输入有误,分数不能大于100");
    }
    else if(score >=90.0 && score <=100.0)
    {
        level = 'A';
        printf("你分数对应的等级为:%c\n",level);
    }
    else if(score >=60.0 && score < 90.0)
    {
        level = 'B';
        printf("你分数对应的等级为:%c\n",level);
    }
    else
    {
        level = 'C';
        printf("你分数对应的等级为:%c\n",level);
    }
    system("pause");
}

In this modified code, if the input score is less than 0, the output is "input error, score cannot be less than 0"; If the input score is greater than 100, it will say "incorrect input, score cannot be greater than 100". If the score entered is between 0 and 100, the corresponding grade will be printed.

About the Author

Question Info

Publish Time
2023-02-28 20:23
Update Time
2023-02-28 20:23