掷骰子游戏

    编写函数模拟掷骰子的游戏(两个骰子)。第一次掷的时候,如果点数之和为7或11则获胜;如果点数之和为2、3或12则落败;其他情况下的点数之和称为“目标”,游戏继续。在后续的投掷中,如果玩家再次掷出“目标”点数则获胜,掷出7则落败,其他情况都忽略,游戏继续进行。每局游戏结束时,程序询问用户是否再玩一次,如果用户输入的回答不是y或Y,程序会显示胜败的次数然后终止。


#include<stdio.h>
#include<stdlib.h>
#include<time.h>


int main()
{
	int winCount, lostCount; 
	int first, second;
	int target;
	char input;

	winCount=lostCount=0;

	srand(time(0));
	first=rand()%6+1;
	second=rand()%6+1;

	printf("%d %d\n",first,second);


	target=first+second;

	if(target==7 || target==11)
	{
		printf("you win\n");
		winCount++;
	}
	else
	{
		if(target==2 || target==3 || target==12)
		{
			printf("you lost\n");
			lostCount++;
		}
		else
		{
			printf("proceed or not?");
			input=getchar();

			while(input=='Y' || input=='y')
			{
				first=rand()%6+1;
				second=rand()%6+1;

				printf("%d %d\n",first,second);
	
				if(target==first+second){
					printf("you win\n");
					winCount++;
				}
				else
					if (first+second==7){
						printf("you lost\n");
						lostCount++;
					}
				
				printf("proceed or not?");
				getchar();
				input=getchar();
			}
		}
	}

	printf("win=%d, lost=%d\n",winCount, lostCount);
	return 0;

}




  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值