关于设计一个C语言石头剪刀布游戏问题

#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 win=0,lose=0,he=0;
srand(time(NULL));
printMenu();
while(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("石头");break;
case '2': printf("剪刀");break;
case '3': printf("布");break;
}
switch(ch-Computer)
{
case 0: printf("\t平手!\n"); he++; break;
case -1: case 2: printf("\t你赢了!\n"); win++; break;
case 1: case -2: printf("\t你输了!\n"); lose++; break;
}
}
printf("\n您一共玩了%d次。\n",win+lose+he);
printf("胜利:%d 次\n",win);
printf("失败:%d 次\n",lose);
printf("平手:%d 次\n",he);
return 0;
}
这是我目前找到 希望能将此程序改成三局两胜制 最后只要输出三局之后玩家是否输赢就好了

这个可以啊,我改了下,你运行一下
#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 win=0,lose=0,he=0;
int flag=0;
srand(time(NULL));
printMenu();
while(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("石头");break;
case '2': printf("剪刀");break;
case '3': printf("布");break;
}
switch(ch-Computer)
{
case 0: printf("\t平手!\n"); he++; break;
case -1: case 2: printf("\t你赢了!\n"); win++; break;
case 1: case -2: printf("\t你输了!\n"); lose++; break;
}

if(win>=2)
{
printf("这场比赛你赢了,恭喜你\n");
break;
}
else if(lose>=2)
{
printf("很遗憾,这场比赛你输了\n");
break;
}

}
printf("\n您一共玩了%d次。\n",win+lose+he);
printf("胜利:%d 次\n",win);
printf("失败:%d 次\n",lose);
printf("平手:%d 次\n",he);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-08-29
这是我改的,希望对你有帮助,哈哈!!!
#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;
}本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网