猜数字游戏代码实现(带有注释)

猜数字游戏

//猜数字游戏
void menu() {
	printf("=========================\n");
	printf("======  1. play   =======\n");
	printf("======  0. exit   =======\n");
	printf("=========================\n");
}

//游戏主体
void game()
{
	int guess = 0;//存放用户猜的数字
	//1,生成随机数
	//因为单纯的调用rand,会使用唯一的种子,这样的会生成的数,就是伪随机数。有一定的规律
	int num = rand() % 100 + 1;//调用rand函数生成1~100之间的随机数
	//2,猜数字
	while (1) {
		printf("请猜数字:\n");
		scanf("%d", &guess);
		if (guess > num) {
			printf("抱歉,猜大了~~\n");
		}
		else if (guess < num) {
			printf("抱歉,猜小了~~\n");
		}
		else {
			printf("恭喜你猜对了,你真的太棒了~~~\n");
			break;//跳出循环
		}
	}
}
int main(void)
{
	int input = 0;//控制进入游戏的变量
	srand((unsigned int)time(NULL));//利用系统时间来为rand提供种子
	do {
		//打印菜单
		menu();//调用函数打印菜单
		printf("请输入‘1’或‘0’进行选择\n");//输出提示
		scanf("%d", &input);//用户输入
		switch (input) {
		case 1:
			game();//输入1,进入游戏
			break;
		case 0:
			printf("谢谢,退出游戏~\n");//输入0退出游戏
		default :
			printf("输出错误,请重新输入‘1’或‘0’进行选择");//如果输入结果不是0或1进行提示
		}
	}while (input);

	return 0;
}

这是最基本的模式,可以在此基础上添加条件来限制用户的输入次数。可以将game函数更改为

void game()
{
	int guess = 0;//存放用户猜的数字
	//1,生成随机数
	//因为单纯的调用rand,会使用唯一的种子,这样的会生成的数,就是伪随机数。有一定的规律
	int num = rand() % 100 + 1;//调用rand函数生成1~100之间的随机数
	int count = 5;//利用count来控制输入的次数
	//2,猜数字
	while (count) {
		printf("你还有%d次机会\n", count);
		printf("请猜数字:\n");
		scanf("%d", &guess);
		if (guess > num) {
			printf("抱歉,猜大了~~\n");
		}
		else if (guess < num) {
			printf("抱歉,猜小了~~\n");
		}
		else {
			printf("恭喜你猜对了,你真的太棒了~~~\n");
			break;//跳出循环
		}
		count--;
	}
	if (count == 0) {
		printf("非常抱歉,您的机会已经使用完了~,正确的值是:%d\n", num);
	}
}

在这里插入图片描述
当然也是可以更加的有趣的,比如添加倒计时之类的,这边将会再演示一个较为有趣的办法,先看代码,一定要仔细观察

void game()
{
	int guess = 0;//存放用户猜的数字
	//1,生成随机数
	//因为单纯的调用rand,会使用唯一的种子,这样的会生成的数,就是伪随机数。有一定的规律
	int num = rand() % 100 + 1;//调用rand函数生成1~100之间的随机数
	int count = 5;//利用count来控制输入的次数
	//2,猜数字
	while (count) {
		printf("你还有%d次机会\n", count);
		printf("请猜数字,但是不要轻易的相信提示呦:\n");
		scanf("%d", &guess);
		if (guess < num) {
			printf("抱歉,猜大了~~\n");//实际上是猜小了
		}
		else if (guess > num) {
			printf("抱歉,猜小了~~\n");//实际上是猜大了
		}
		else {
			printf("恭喜你猜对了,你真的太棒了~~~\n");
			break;//跳出循环
		}
		count--;
	}
	if (count == 0) {
		printf("非常抱歉,您的机会已经使用完了~,正确的值是:%d\n", num);
	}
}

结果是这样的:在这里插入图片描述
也就是说,你猜的数大了,但是实际上会显示小了,这不是一种特别的趣味嘛~~~~

  • 13
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
guessing and betting number game The user starts with 100 dollars, and is able to bet and guess until they quit or have no more money. In each turn, they are asked for a bet. If they enter 0, then the program should give a farewell message and quit. The bet must be positive and within their available balance, If they enter incorrectly, the program should set the bet to their balance and tell them this. Once a bet is entered, they must pick a number between 1 and 10 (you do not need to errorcheck or correct this). This guess is then compared to a number the computer randomly generates each time (also between 1 and 10). If the guess is correct, the user wins the amount of their bet. If the guess is incorrect, the user will lose their bet divided by 5 and multiplied by the distance from the correct number - e.g. if the bet is 50, the computer’s number is 5 and the user guesses 7, then the user will lose 20 dollars( 50/5 * (7-5) = 20 ). However, they should not lose more than their balance, so if their balance is 50, the bet is 40, the computer’s number is 1 and the user guesses 10, (40/5 * (10-1) = 72 > 50 )then they should lose 50. After each turn, the program should display that turn's winnings (or loss amount) and the new balance,and then repeat the game until the user either quits or has no money left. Dollar values should be formatted in the standard way with two decimal places and a $ in front. Sample output from the program is below. You should make your program match this exactly (except for your name). An appropriate welcome message with your name in it should be shown at the start of the program. Assignment 1 : Guessing Game Written by yourname Please enter your bet (up to $100.00): $50 Guess a number between 1 and 10: 5 Correct! You win $50.00 Your new balance is $150.00 Please enter your bet (up to $150.00): $200 Your bet is $150.00 Guess a number between 1 and 10: 6 Wrong! The computer chose: 4 You lose $60.00 Your new balance is $90.00 Please enter your bet (up to $90.00): $80 Guess a number between 1 and 10: 1 Wrong! The computer chose: 7 You lose $90.00 Thank you for playing! Press any key to continue . . . Another Sample: CP1200 Guessing Game Written by Lindsay Ward Please enter your bet (up to $100.00): $-20 Your bet is $100.00 Guess a number between 1 and 10: 5 Wrong! The computer chose: 2 You lose $60.00 Your new balance is $40.00 Please enter your bet (up to $40.00): $10.50 Guess a number between 1 and 10: 12 Wrong! The computer chose: 2 You lose $21.00 Your new balance is $19.00 Please enter your bet (up to $19.00): $0 Thank you for playing! Press any key to continue . . . srand( static_cast<int>(time(0))) ; 初始化 computerNumber = 1 + rand( ) % 10 ; 产生随机数 cout << fixed << setprecision(2) ; 控制输出显示2位小数
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十月三十二号的风情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值