帮忙看下这个c++代码

有一函数: y=x (x<1) y=3x-1 (1<=x<10) y=4x-2 (x>=10) 编写程序,输入x,输出y的值这个是我写的代码:#include<stdio.h>int main(){ int x,y; scanf("%d",&x); if(x<1) y=x; else if(x>=10) y=4x-2; else y=3x-1; printf("%d尀n,y); return 0;}编译结果如下:Compiling...Cpp1.cppC:尀Users尀Tom尀Desktop尀Cpp1.cpp(9) : error C2059: syntax error : ✀bad suffix on number✀C:尀Users尀Tom尀Desktop尀Cpp1.cpp(9) : error C2146: syntax error : missing ✀;✀ before identifier ✀x✀C:尀Users尀Tom尀Desktop尀Cpp1.cpp(9) : warning C4552: ✀-✀ : operator has no effect; expected operator with side-effectC:尀Users尀Tom尀Desktop尀Cpp1.cpp(10) : error C2181: illegal else without matching ifC:尀Users尀Tom尀Desktop尀Cpp1.cpp(11) : error C2059: syntax error : ✀bad suffix on number✀C:尀Users尀Tom尀Desktop尀Cpp1.cpp(11) : error C2146: syntax error : missing ✀;✀ before identifier ✀x✀C:尀Users尀Tom尀Desktop尀Cpp1.cpp(11) : warning C4552: ✀-✀ : operator has no effect; expected operator with side-effectC:尀Users尀Tom尀Desktop尀Cpp1.cpp(12) : error C2001: newline in constantC:尀Users尀Tom尀Desktop尀Cpp1.cpp(13) : error C2143: syntax error : missing ✀)✀ before ✀return✀C:尀Users尀Tom尀Desktop尀Cpp1.cpp(14) : warning C4508: ✀main✀ : function should return a value; ✀void✀ return type assumed执行 cl.exe 时出错.刚开始学,问题比较多,谢谢了
2026年09月25日 04:29
有3个网友回答
网友(1):

y=4x-2; 和 y=3x-1; 应分别写为 y=4*x-2; 和 y-3*x-1;
乘号不可省略。

网友(2):

代码输入错误:
y=4x-2;
应该改为:y=4*x-2;
以下类推。

网友(3):

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