大一C语言,我严重不理解!


#include<stdio.h>
void main()
{
    double x;
    while(scanf("%lf",&x)!=EOF)
    {
        double y;
    if(x<1)
    {
        y=x;
        printf("y = %.0lf\n",y);
    }
    else if(1<=x<10)
    {
        y=2*x-1;
        printf("y = %.0lf\n",y);
    }
    else
    {
        y=10;
        printf("y = %.0lf\n",y);
    }
    }
}

改之前的代码如上,运行不正确。
else if那里的条件和else那里的条件对换后,运行就正确了,我不懂为什么!
改后如下:

#include<stdio.h>
void main()
{
    double x;
    while(scanf("%lf",&x)!=EOF)
    {
        double y;
    if(x<1)
    {
        y=x;
        printf("y = %.0lf\n",y);
    }
    else if(x>10)
    {
        y=10;
        printf("y = %.0lf\n",y);
    }
    else
    {
        y=2*x-1;
        printf("y = %.0lf\n",y);
    }
    }
}

else if(1<=x<10)
if语句这么写是错的,改成else if(x>=1 && x<10)即可

我虽然不是学c的,但是学了一年前端之后,以前的不了解都觉得好弱智。加油

你好,这里是c语言,不是数学,谁教你这样写了1<=x<10?

// 运行到else if的时候,x 一定是不满足x < 1的,所有只写x<10就可以了
else if(1<=x<10) //另外,代码中不能用通常的数学写法1<=x<10这样的写法,要写1<=x && x<10

if(x<1)
    {
        y=x;
        printf("y = %.0lf\n",y);
    }
    // 运行到else if的时候,x 一定是不满足x < 1的,所有只写x<10就可以了
    else if(1<=x<10) //另外,代码中不能用通常的数学写法1<=x<10这样的写法,要写1<=x && x<10
    {
        y=2*x-1;
        printf("y = %.0lf\n",y);
    }
    else
    {
        y=10;
        printf("y = %.0lf\n",y);
    }