c++中,下面那个程序有哪七处错误啊,在线等!!!!

2025-02-26 09:30:12
推荐回答(4个)
回答1:

这个程序其实最好还是用if语句比较好。如果你非得要用switch的话,就需要再加一个变量。改正后的程序给你参考一下,具体错误就不一一说了,你自己去找错误去学习。
#include
using namespace std;
int main()
{int x,y,sym;
cin>>x;
if(x<1)sym=0;
else if(x>=1&&x<10)sym=1;
else if(x>=10)sym=2;
switch(sym)
{case 0:cout<<"y="< case 1:cout<<"y="<<2*x-1;break;
case 2:cout<<"y="<<3*x-11;break;
}
return 0;
}

回答2:

这错误就多了:
1,y还是随机值,switch(y)就错的,除非你输入y;
2,1<=x<10这是数学上的表达,c++中要这样,1<=x && x < 10,同理,2x -1也要改成2 *x -1,3x-11也一样;
3,switch中,最好在最后加上default:break,在没有满足条件时执行。当然也可以不加,但又是程序会出毛病 ;
4,case后面要跟常量,判断跟在后面结果是有FALSE或者TRUE,y除了是0都是TRUE,这个相当于
switch(y)
{
case FALSE: ...
case TRUE:...
...
}
所以执行不一定是你要的结果。

回答3:

switch 下面不能用表达时 只能是数字 或者字符
你这个还是用if-else吧
还有2x 要写成2*x
给你改好了:
#include
using namespace std;
int main()
{
int x;
cin>>x;
if(x<1) cout<<"y="< else if(x>=1&&x<10) cout<<"y="<<2*x-1;
else if(x>=10) cout<<"y="<<3*x-11;
return 0;
}

回答4:

error C2051: case expression not constant
warning C4804: '<' : unsafe use of type 'bool' in operation
error C2051: case expression not constant
error C2059: syntax error : 'bad suffix on number'
error C2146: syntax error : missing ';' before identifier 'x'
warning C4552: '-' : operator has no effect; expected operator with side-effect
error C2051: case expression not constant
error C2059: syntax error : 'bad suffix on number'
error C2146: syntax error : missing ';' before identifier 'x'
warning C4552: '-' : operator has no effect; expected operator with side-effect
warning C4060: switch statement contains no 'case' or 'default' labels