#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
void printMenu(void)
{
printf("1-石头 2-剪刀 3-布 4-退出\n");
}
int main(void)
{
char ch,Computer;
int i,win=0,lose=0,he=0;
srand(time(NULL));
printMenu();
for(i=0;i<3;i++)
{
printf("第%d场比赛开始",i+1);
printf("您的选择(1、2、3):");
while(1)
{
ch = getch();
if(ch=='1'||ch=='2'||ch=='3'||ch=='4')
{
printf("%c\t",ch);
break;
}
}
if(ch=='4')
break;
switch(ch)
{
case '1': printf("石头 VS ");break;
case '2': printf("剪刀 VS ");break;
case '3': printf("布 VS ");break;
}
Computer = rand()%3+1+48;
switch(Computer)
{
case '1': printf("石头\n");break;
case '2': printf("剪刀\n");break;
case '3': printf("布\n");break;
}
switch(ch-Computer)
{
case 0: he++; break;
case -1: case 2: win++; break;
case 1: case -2: lose++; break;
}
}
printf("比赛结束\n");
if(win==lose)
{
printf("双方打成平手");
}
else if(win>lose)
{
printf("您取得了胜利");
}
else if(win<lose)
{
printf("很遗憾你输了");
}
return 0;
}
#include <conio.h>
#include <stdlib.h>
#include <time.h>
void printMenu(void)
{
printf("1-石头 2-剪刀 3-布 4-退出\n");
}
int main(void)
{
char ch,Computer;
int i,win=0,lose=0,he=0;
srand(time(NULL));
printMenu();
for(i=0;i<3;i++)
{
printf("第%d场比赛开始",i+1);
printf("您的选择(1、2、3):");
while(1)
{
ch = getch();
if(ch=='1'||ch=='2'||ch=='3'||ch=='4')
{
printf("%c\t",ch);
break;
}
}
if(ch=='4')
break;
switch(ch)
{
case '1': printf("石头 VS ");break;
case '2': printf("剪刀 VS ");break;
case '3': printf("布 VS ");break;
}
Computer = rand()%3+1+48;
switch(Computer)
{
case '1': printf("石头\n");break;
case '2': printf("剪刀\n");break;
case '3': printf("布\n");break;
}
switch(ch-Computer)
{
case 0: he++; break;
case -1: case 2: win++; break;
case 1: case -2: lose++; break;
}
}
printf("比赛结束\n");
if(win==lose)
{
printf("双方打成平手");
}
else if(win>lose)
{
printf("您取得了胜利");
}
else if(win<lose)
{
printf("很遗憾你输了");
}
return 0;
}