Linux C一站式学习习题答案 剪刀石头布

本文介绍了一个简单的石头剪刀布游戏程序实现。通过C语言,该程序实现了玩家与计算机之间的交互,包括用户输入验证、随机数生成以及游戏结果判断等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

char arr[3][10] = { "scissor", "stone", "cloth"};
int man, comp, result;
int man_w = 0;
int	com_w = 0;

int get_man()
{
	printf("Please chose your gesture: 0-scissor,1-stone,2-cloth\n");
	scanf("%d",&man);
	if (man < 0 || man > 2) {
		man = get_man();
	}
	return man;
}

void game()
{
	srand(time(NULL));

	//用户选择出什么
	man = get_man();

	//电脑随机出
	comp = rand() % 3;

	//输出结果
	printf("your gesteure is %s\n", arr[man]);
	printf("comp's gesteure is %s\n", arr[comp]);

	//比较输赢
	result = (man - comp + 4) % 3 - 1;

	if (result > 0)
	{
		printf("You Win!!!\n");
		man_w++;
	} else if (0 == result){
		printf("Draw!!!\n");
	} else {
		printf("You Lose!!!\n");
		com_w++;
	}
	return;
}

int main()
{
	int i, N;
	printf("please enter the num you want to play:");
	scanf("%d", &N);
	for (i = 0; i < N; i++)
	{
		printf ("************Round %d*************\n", i+1);
		game();
		printf ("\n\n");
	}

	if (man_w > com_w)
		printf ("Congratulation!\n%d to %d You WIN!\n",man_w, com_w);
	else if (man_w = com_w)
		printf("Draw!!!\n%d to %d\n",man_w, com_w);
	else
		printf ("Sorry,\n%d to %d You lose!Try again?\n",man_w, com_w);
	return 0;
}
注:转载请注明源地址:http://blog.youkuaiyun.com/whorus1/article/list/2,谢谢!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值