猜数字游戏

#define _CRT_SECURE_NO_WARNINGS 1
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STAGE  10
int main()
{
		int no;
		int i = 0;
		int stage = 0;
		int ans;
		int num[MAX_STAGE];
		int select = 0;
		printf("+-----------------------------------------+\n");
		printf("| 1.Easy  2.Moderate  3.Hard  4.More Hard |\n");
		printf("+-----------------------------------------+\n");
		printf("Please Select#");
		scanf("%d", &select);
		switch (select){
		case 1:
            srand((unsigned long)time(NULL));
			ans = rand() % 10 + 1;
			printf("Please guess an integer from 1 to 10.\n\n");
			break;
		case 2:
            srand((unsigned long)time(NULL)); 
			ans = rand() % 100 + 1;
			printf("Please guess an integer from 1 to 100.\n\n");
			break;
		case 3:
            srand((unsigned long)time(NULL));
			ans = rand() % 1000 + 1;
			printf("Please guess an integer from 1 to 1000.\n\n");
			break;
		case 4:
            srand((unsigned long)time(NULL)); 
			ans = rand() % 10000 + 1;
			printf("Please guess an integer from 1 to 10000.\n\n");
			break;
		default:
			printf("Your Enter Error,Try again!\n\n\n");
			break;
		}
		do{
			printf("You have %d more chances ,Please Guess>", MAX_STAGE - stage);
			scanf("%d", &no);
			num[stage++] = no;
			if (no > ans)
				printf("big!!!\n");
			else if (no < ans)
				printf("small!!!\n");
		} while (no != ans&&stage < MAX_STAGE);
		if (no != ans)
			printf("Unfortunately, the correct answer is %d\n", ans);
		else{
			printf("Guess success!\n");
			printf("You got it right %d times\n", stage);
		}
		puts("\n--- Input record ---");
		for (i = 0; i < stage; i++){
			printf(" %2d : %4d %+4d\n", i + 1, num[i], num[i] - ans);
		}
	system("pause");
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void Guess()
{
	int x;
	int select = 0;
        printf("+-----------------------------------------+\n");
        printf("| 1.Easy  2.Moderate  3.Hard  4.More Hard |\n");
	printf("+-----------------------------------------+\n");
	printf("Please Select#");
	scanf("%d", &select);
	switch (select){
	case 1:
		srand((unsigned long)time(NULL));
		x = rand() % 10 + 1;
		printf("Please guess an integer from 1 to 10.\n\n");
		break;
	case 2:
		srand((unsigned long)time(NULL));
		x = rand() % 100 + 1;
		printf("Please guess an integer from 1 to 100.\n\n");
		break;
	case 3:
		srand((unsigned long)time(NULL));
		x = rand() % 1000 + 1;
		printf("Please guess an integer from 1 to 1000.\n\n");
		break;
	case 4:
		srand((unsigned long)time(NULL));
		x = rand() % 10000 + 1;
		printf("Please guess an integer from 1 to 10000.\n\n");
		break;
	default:
		printf("Your Enter Error,Try again!\n\n\n");
		break;
	}
	int g = 0;
	while (1){
		printf("Please Guess>");
		scanf("%d", &g);
			if (g == x){
				printf("guess success!\n");
				break;
			}
			else if (g > x){
				printf("big!\n");
			}
			else{
				printf("small!\n");
			}
	}
}
void Game()
{
	int quit = 0;
	while (!quit){
		int select = 0;
		printf("+-------------------+\n");
		printf("|  1.Play   2.Exit  |\n");
		printf("+-------------------+\n");
		printf("Please Select#");
		scanf("%d", &select);
		switch (select){
		case 1:
			Guess();
			break;
		case 2:
			quit = 1;
			break;
		default:
			printf("Your Enter Error,Try again!\n\n\n");
			break;
		}
	}
}
int main()
{
	Game();
	system("pause");
	return 0;
}

第一份代码参考于柴田望洋的《明解C语言·中级篇》,我给它增加了猜数字游戏的难度选择选项,但是不知道怎样让它一直循环下去。第二份代码可以一直循环下去,但是如果可以给每个难度选项增加不同的猜数字次数就更好了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

怎样让大排不硬

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

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

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

打赏作者

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

抵扣说明:

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

余额充值