| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 729 人关注过本帖
标题:用VC++编的猜拳游戏,能运行但是有些小问题,望指正!
只看楼主 加入收藏
elwin_zhou
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-6-24
收藏
 问题点数:0 回复次数:2 
用VC++编的猜拳游戏,能运行但是有些小问题,望指正!

代码如下,其他运行正常,但是程序到最后会跳过问“还想继续玩吗?”这个步骤,不知道是什么道理,望指正!谢谢!
本人是C语言初学者,见笑了
#include <stdio.h>
#include <stdlib.h>
int play(int p,int c) /*p代表people,c代表computer*/
{
if (p==1&&c==2||p==2&&c==3||p==3&&c==1)
return 1;
else if (p==2&&c==1||p==3&&c==2||p==1&&c==3)
return 2;
else if (p==1&&c==1||p==2&&c==2||p==3&&c==3)
return 3;
}
void main()
{
int p,c,result;
char replay;
printf("\n====================猜拳游戏===================\n");
for(;;)
{
c=rand()%3;
printf("请输入你的选择:(1--石头 2--剪刀 3--布 )");
scanf("%d",&p);
printf("\n你的选择是");

if(p==1)
printf("石头\n");
else if(p==2)
printf("剪刀\n");
else if(p==3)
printf("布\n");
else
{
printf("错误的数字!\n");
continue;
}
printf("我的选择是");
if(c==1)
printf("石头\n");
else if(c==2)
printf("剪刀\n");
else if(c==3)
printf("布\n");

result=play(p,c);
if(result==1)
printf("恭喜你,你赢了~!\n");
else if(result==2)
printf("很遗憾,你输了~!\n");
else if(result==3)
printf("平局\n");
printf("还想继续玩吗?(y/n)\n\n");
scanf("%c",&replay);
if(replay=='y')
continue;
else if(replay=='n')
break;
}
}

[此贴子已经被作者于2006-7-14 18:10:05编辑过]

搜索更多相关主题的帖子: 猜拳 游戏 运行 
2006-07-14 18:07
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

你的程序基本没错,只是在
printf("还想继续玩吗?(y/n)\n\n");
scanf("%c",&replay);
加一条语句:getchar();以便接收以前的回车符.就可以了.
还值得一提的是:

void main()
{
int p,c,result,flag=1;//标记
char replay;
printf("\n====================猜拳游戏===================\n");
while(flag)
{
c=rand()%3;
printf("请输入你的选择:(1--石头 2--剪刀 3--布 )");
scanf("%d",&p);
printf("\n你的选择是");

if(p==1)
printf("石头\n");
else if(p==2)
printf("剪刀\n");
else if(p==3)
printf("布\n");
else
{
printf("错误的数字!\n");
continue;
}
printf("我的选择是");
if(c==1)
printf("石头\n");
else if(c==2)
printf("剪刀\n");
else if(c==3)
printf("布\n");

result=play(p,c);
if(result==1)
printf("恭喜你,你赢了~!\n");
else if(result==2)
printf("很遗憾,你输了~!\n");
else if(result==3)
printf("平局\n");
printf("还想继续玩吗?(y/n)\n\n");
getchar();
scanf("%c",&replay);
if(replay=='y')
flag=1;
else if(replay=='n')
flag=0;
}
}

这样可能会好点(个人意见!呵呵)


倚天照海花无数,流水高山心自知。
2006-07-14 18:38
elwin_zhou
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-6-24
收藏
得分:0 

经过修改,已经运行正常,谢谢啦~!!!

2006-07-14 19:47
快速回复:用VC++编的猜拳游戏,能运行但是有些小问题,望指正!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019866 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved