C程序打印范围

时间:2017-03-07 08:43:03

标签: c printf range control-flow

我试图用一个简单的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 +(我在其余的#34;限制&#34;我的范围内也有同样的问题)。有人可以帮我解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

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

使用

else if(grade>=3.3f)

而不是

else if(grade>=3.3)