编写一个C程序,输入x的值,输出y相应的表达式与值

根据

img


编写一个C程序,输入x的值,输出y相应的表达式与值
问题如下

img

img


不知道哪里错了,希望大家能帮忙解决一下,谢谢了。

大哥你在第8行代码改为:else if(x>=1&&x<10),并且来判断大于等1,小于10

img


完整代码为:

#include <stdio.h>
int main()
{
    int x,y;
    scanf("%d",&x);
    if(x<1)
    {
        y=x;
        printf("y=x=%d",y);    
    }
    else if(x<10)
    {
        y=2*x-1;
        printf("y=2x-1=%d",y);
    }
    else if(x>=10)
    {
        y=3*x-11;
        printf("y=3x-11=%d",y);
    }
    return 0;
}

应该是else if(x>=1&&x<10)吧

#include <stdio.h>
int main()
{
    int x,y;
    scanf("%d",&x);
    if(x<1)
    {
        y=x;
        printf("y=x=%d",y);    
    }
    else if(x>=1&&x<10)
    {
        y=2*x-1;
        printf("y=2*x-1=%d",y);
    }
    else if(x>=10)
    {
        y=3*x-11;
        printf("y=3*x-11=%d",y);
    }
    return 0;
}

if条件写错了 1<=x<10 需要用&&表示并且关系 if(x>=1&&x<10) 或者直接else if(x<10)就行了,因为前边if已经判断x<1了

#include <stdio.h>
int main()
{
    int x,y;
    scanf("%d",&x);
    if(x<1)
    {
        y=x;
        printf("y=x=%d",y);    
    }
    else if(x<10)
    {
        y=2*x-1;
        printf("y=2x-1=%d",y);
    }
    else if(x>=10)
    {
        y=3*x-11;
        printf("y=3x-11=%d",y);
    }
    return 0;
}