直接上代码
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
// 任务1 玩家出招
cout << "和AI玩石头剪刀布,请输入你的选择:"<< endl;
cout << "1、石头" << endl;
cout << "2、剪刀" << endl;
cout << "3、布" << endl;
int a;
cin >> a;
cout << "你的选择是:" << a << endl;
if(a == 1)
{
cout << "你出的石头" << endl;
}
if(a == 2)
{
cout << "你出的剪刀" << endl;
}
if(a == 3)
{
cout << "你出的布" << endl;
}
// 任务2 AI出招
srand(time(0)); //评测时请注释掉这行数据
int b = rand()%3 +1;
if(b == 1)
{
cout << "AI出的石头" << endl;
}
if(b == 2)
{
cout << "AI出的剪刀" << endl;
}
if(b == 3)
{
cout << "AI出的布" << endl;
}
// 任务3 判断胜负
// 使用if嵌套来完成判断
if(b==a)
{
cout<<"平手"<<endl;
}
else
{
if(a<b)
{
if(b-a==2)
{
cout<<"你输了";
}
else
{
cout<<"你赢了";
}
}
else{
if(a-b==2)
{
cout<<"你赢了";
}
else
{
cout<<"你输了";
}
}
}
return 0;
}