主题:剪刀、石头、布,为什么输入P,布的时候总出错??
r代表rock石头,s代表scissor 剪刀,p代表paper布,为什么在选输入p的时候情况总出错,哪里出问题了,求解释。
#include "stdafx.h"
#include "cmath"
#include "iostream"
#include "cctype"
int whowin(char,char);
char r='r',s='r',p='p',me,you,c;
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
do
{
cout<<"我出:"<<endl;
cin>>me;me=tolower(me);
cout<<"你出:"<<endl;
cin>>you;you=tolower(you);
if(whowin(me,you)==1)
cout<<"我赢了"<<endl;
else if(whowin(me,you)==-1)
cout<<"你赢了"<<endl;
else if(whowin(me,you)==0)
cout<<"平手"<<endl;
cout<<"是否继续"<<endl;
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}
int whowin(char a,char b)
{
using std::cout;
switch(a)
{
case 'r':
switch(b)
{
case 'r':return 0;//break;
case 's':return 1;//break;
case 'p':return -1;//break;
}
case 's':
switch(b)
{
case 'r':return -1;//break;
case 's':return 0;//break;
case 'p':return 1;//break;
}
case 'p':
switch('b')
{
case 'r':return -1;//break;
case 's':return 1;//break;
case 'p':return 0;//break;
}
default :cout<<"不应该出现这种情况";
}
}
#include "stdafx.h"
#include "cmath"
#include "iostream"
#include "cctype"
int whowin(char,char);
char r='r',s='r',p='p',me,you,c;
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
do
{
cout<<"我出:"<<endl;
cin>>me;me=tolower(me);
cout<<"你出:"<<endl;
cin>>you;you=tolower(you);
if(whowin(me,you)==1)
cout<<"我赢了"<<endl;
else if(whowin(me,you)==-1)
cout<<"你赢了"<<endl;
else if(whowin(me,you)==0)
cout<<"平手"<<endl;
cout<<"是否继续"<<endl;
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}
int whowin(char a,char b)
{
using std::cout;
switch(a)
{
case 'r':
switch(b)
{
case 'r':return 0;//break;
case 's':return 1;//break;
case 'p':return -1;//break;
}
case 's':
switch(b)
{
case 'r':return -1;//break;
case 's':return 0;//break;
case 'p':return 1;//break;
}
case 'p':
switch('b')
{
case 'r':return -1;//break;
case 's':return 1;//break;
case 'p':return 0;//break;
}
default :cout<<"不应该出现这种情况";
}
}