C 解决猜数字游戏,由系统随机产生一个数字,然后用户输入一个数字来猜测

该程序是一个C语言编写的猜数字游戏,系统生成一个90到100的随机数,用户有10次机会猜测。如果在3次内猜中,显示GoodGuess,超过10次未猜中或输入负数则显示GameOver,并告知剩余尝试次数。
摘要由CSDN通过智能技术生成

C 解决猜数字游戏,由系统随机产生一个数字,然后用户输入一个数字来猜测
1、输入的数字比随机产生的大提示“Too big”,小了提示“Too small”
2、若在3次之内猜中了,输出“Good Guess”
3、超过N次没猜中,提示“Game Over”,在N次之前输入一个负数也提示“Game over”
思路:

用一个while(1)循环
    (1)当猜测次数超过N次或输入负数时,break
    (2)否则,根据猜测的数据输出相应的提示

代码:

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#include<stdlib.h>


int main()
{
	/* 
	* 猜数字游戏,由系统随机产生一个数字,然后用户输入一个数字来猜测
	* 输入的数字比随机产生的大提示“Too big”,小了提示“Too small”
	* 若在3次之内猜中了,输出“Good Guess”
	* 超过N次没猜中,提示“Game Over”,在N次之前输入一个负数也提示“Game over”
	*/
	srand((unsigned)time(NULL)); // 为了每次产生的随机数不同
	int random = rand() % 11 + 90; // rand() % a + b代表的时[b,a+b-1]之间的数字
	int N = 10;
	int count = 0;
	int gussNum;
	
	//printf("%d ", random);
	while (1) {
		scanf_s("%d", &gussNum);
		count++;
		if (count > N ) {
			printf("Game Over,the remaining number of times is 0\n");
			break;
		}

		if (gussNum < 0) {
			printf("Game Over,input data is less than 0\n");
		}
		
		
		if (gussNum == random) {
			if (N <= 3) {
				printf("Good Guess,less than three times\n");
			}
			else {
				printf("Guess is right,Game over,the total number of gusses is %d\n",count);
				break;
			}
		}
		else if (gussNum > random) {
			printf("Too big,the remaining number of times is %d\n", N - count);
		}
		else {
			printf("Too small,the remaining number of times is %d\n", N - count);
		}
	}
	return 0;
}

运行结果截图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值