注册 登录
编程论坛 C语言论坛

可以帮忙优化一下吗

门中人ysl 发布于 2023-10-16 11:34, 551 次点击
#include<stdio.h>
int main()
{
    double t;
    while(scanf("%lf",&t) !=EOF)
{
   
    if(t > 100)
         printf("Score is error!");
            
    else if(t <= 100 && t>=90)
        printf("A");
        
    else if(t <= 90 && t>=80)
        printf("B");
        
    else if(t <= 80 && t>=70)
        printf("C");
        
    else if(t <= 70 && t>=60)
        printf("D");
        
   else if(t <=60 && t>=0)
        printf("E");

    }
   
    return 0;
}


2 回复
#2
yiyanxiyin2023-10-16 12:03
#include<stdio.h>
int main()
{
    double t;
    while(scanf("%lf",&t) !=EOF)
{
   
    if(t > 100)
         printf("Score is error!");
            
    else if(t <= 100 && t>=90)
        printf("A");
        
    else if(t < 90 && t>=80)
        printf("B");
        
    else if(t < 80 && t>=70)
        printf("C");
        
    else if(t < 70 && t>=60)
        printf("D");
        
   else if(t <60)
        printf("E");

    }
   
    return 0;
}
#3
rjsp2023-10-16 16:07
主要是 if 的条件重复了;另外,t<0 的情况你没处理

程序代码:
#include <stdio.h>

int main( void )
{
    for( double t; scanf("%lf",&t)==1; )
    {
        if( t>100 || t<0 )
            puts( "Score is error!" );
        else if( t >= 90 )
            puts( "A" );
        else if( t >= 80 )
            puts( "B" );
        else if( t >= 70 )
            puts( "C" );
        else if( t >= 60 )
            puts( "D" );
        else
            puts( "E ");
    }
}


程序代码:
#include <stdio.h>

int main( void )
{
    for( double t; scanf("%lf",&t)==1; )
    {
        if( t>100 || t<0 )
            puts( "Score is error!" );
        else
            printf( "%c\n", "EEEEEEDCBAA"[ ((int)t)/10 ] );
    }
}

1