C程序打印范围

C program printing a range

我正在尝试编写一个简单的 C 程序来打印成绩:

int main(void)    
{

    float grade;
    int test;
    printf("Enter your grade:\n");
    test=scanf("%f", &grade);
    if(test!=1 || grade<0.0)
        printf("Error: Please enter a valid grade\n");
    else
    {
        if(grade>4.0)
            printf("Your grade is A+\n");
        else if(grade == 4.0)
            printf("Your grade is A\n");
        else if(grade>=3.7)
            printf("Your grade is A-\n");
        else if(grade>=3.3)
            printf("Your grade is B+\n");
        else if(grade>=3.0)
            printf("Your grade is B\n");
        else if(grade>=2.7)
            printf("Your grade is B-\n");
        else if(grade>=2.3)
            printf("Your grade is C+\n");
        else if(grade>=2.0)
            printf("Your grade is C\n");
        else if(grade>=1.7)
            printf("Your grade is C-\n");
        else if(grade>=1.3)
            printf("Your grade is D+\n");
        else if(grade>=1.0)
            printf("Your grade is D\n");
        else
            printf("Your grade is F\n");
    }

}

然而,当我按 3.3 时,它打印 B 而不是 B+(我在范围内的 "limits" 的其余部分也有同样的问题)。有人可以帮我解决这个问题吗?谢谢!

因为您将 float 值与 double 类型进行比较。

使用

else if(grade>=3.3f)

而不是

else if(grade>=3.3)