这种代码为何不合法...问题出在红色字的一行

漂移的Rain 2009-05-06 19:56

[i=s] 本帖最后由 漂移的Rain 于 2009-5-6 20:02 编辑 [/i]

#include
#include
#include
using namespace std;
int main(){
srand(time(NULL));
int x=rand()%10;
int type;
cin>>type;
switch (type){
case 1:cout<<"+";break;
case 2:cout<<"-";break;
default :cout<<"Error";break;
}
int y=rand()%10;
cout<
int answer;
cin>>answer;
if (answer= x switch(type) y) cout<<"good";
else cout<<"Error";
}


我的想法是通过switch按照使用者意愿产生+或-...然后产生两个随机数x,y,让使用者计算x+y或者x-y的结果...红色那一行要怎么改~

humboldtpenguin 2009-05-07 13:54

switch()似乎不能直接用来打印...也不能当成运算符号....
不止要改红色那行...最好整个改掉...
把计算过程全放到switch()里头....

小光 2009-05-07 20:21

switch不是函数 不能返回值,坚持要用到话就自己定义个函数吧

把switch放进去