#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
using namespace std;
int main() {
int game;//玩家出手
int computer,gamer;//电脑出手
int result;//比赛结果
//为了让游戏可以多次游玩,把代码全部放入循环之中
while (1)//这个while判定是哪来的
{
printf("这是一个石头剪刀布,请输入你要出的选项:\n");
printf("1:剪刀\n2:石头\n3:布\n4:不玩了\n");
scanf("%d", &game);//%*c 的意思是读取这个输入,但不赋予任何变量
switch (game)
{
case 1:
gamer = 4;
break;
case 2:
gamer = 7;
break;
case 3:
gamer = 10;
break;
case 4:
return 0;
default:
cout << "输入无效,请按规范输入\n";
break;
return 0;
}
srand((unsigned)time(NULL));//产生一个根据系统时钟而输出的随机数
computer = rand() % 3;//产生随机数并取余,得到电脑出拳
printf("电脑出了");
switch (computer)
{
case 0:
printf("剪刀\n");
break;
case 1:
printf("石头\n");
break;
case 2:
printf("布\n");
break;
}
printf("你出了");
switch (gamer)
{
case 4:
printf("剪刀\n");
break;
case 7:
printf("石头\n");
break;
case 10:
printf("布\n");
break;
}
result = (int)gamer + computer;//gamer为char性函数,数学运算的时候要强制进行转化类型
if (result == 6 || result == 7 || result == 11)
printf("你赢啦,恭喜!");
else if (result == 5 || result == 9 || result == 10)
printf("电脑赢了,遗憾~");
else printf("平手");
system("pause>nu&&cls");//暂停并且清屏
}
return 0;
}
12-29
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交