石头剪刀布和猜数字的C语言代码实现

猜数字:

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

int MAX_Number = 100;
int game();

int main()
{
	int replay = 0;
	do {
		game();
		printf("要继续吗?继续请按1并回车,按任意键并回车将退出\n");
		scanf_s("%d", &replay);
	} while (replay == 1);
	return 0;
}

int game() {
	srand(time(NULL));
	int random_number = rand() % (101);
	int max_times = 7;
	printf("我现在生成了一个100以内的正整数,请你猜猜看?你有%d次机会\n", max_times);
	int user_number;
	while (max_times) {
		scanf_s("%d", &user_number);
		--max_times;
		if (user_number < random_number) {
			printf("小了,你还有%d次机会\n", max_times);
		}
		else if (user_number > random_number) {
			printf("大了,你还有%d次机会\n", max_times);
		}
		else if (user_number == random_number) {
			printf("太棒了,你猜中了!\n");
			return 0;
		}
	}
	printf("很遗憾,你没有猜中,正确的数字是%d\n", random_number);
	return 0;
}

石头剪刀布:

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

const char *JD = "剪刀";
const char *ST = "石头";
const char *BU = "布";
const char *LIB[] = {JD, ST, BU};


int main()
{
	int replay = 0;
	srand(time(NULL));
	do {
		int i = rand() % 3;
		int j;
		while (true) {
			printf("请输入1至3之间任意一个数字,其中1代表剪刀,2代表石头,3代表布。\n");
			scanf_s("%d", &j);
			if (j >= 1 and j <= 3) {
				break;
			}
			else {
				printf("你的输入错误!");
				continue;
			}
		}
		switch (j - 1) {
		case (0):
			printf("你出的剪刀!");
			break;
		case (1):
			printf("你出的石头!");
			break;
		case (2):
			printf("你出的布!");
			break;
		}
		switch (i) {
		case (0):
			printf("我出的剪刀!");
			break;
		case (1):
			printf("我出的石头!");
			break;
		case (2):
			printf("我出的布!");
			break;
		}
		if (j - 1 == 0 and i == 0) printf("平局!");
		else if (j - 1 == 0 and i == 1) printf("我赢了!");
		else if (j - 1 == 0 and i == 2) printf("你赢了!");
		else if (j - 1 == 1 and i == 0) printf("你赢了!");
		else if (j - 1 == 1 and i == 1) printf("平局!");
		else if (j - 1 == 1 and i == 2) printf("我赢了!");
		else if (j - 1 == 2 and i == 0) printf("我赢了!");
		else if (j - 1 == 2 and i == 1) printf("你赢了!");
		else if (j - 1 == 2 and i == 2)  printf("平局!");
		printf("要继续吗?继续请按1并回车,否则请按任意键后回车。");
		scanf_s("%d", &replay);
	} while (replay == 1);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值