循环结构编写猜数字游戏

本文介绍了如何利用循环和随机数生成实现一个可玩的猜数字游戏,通过Windows时间设置不同难度限制,包括10秒、15秒和20秒的计时。

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

 利用循环结构,我们可以让玩家反复输入数字并比较大小,由此可以实现基本的猜数字功能。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int a,b=10;
    while (cin >> a)
    {
        if (a > b)
        {
            cout << "偏大" << endl;
        }
        else if (a < b)
        {
            cout << "偏小" << endl;
        }
        else
        {
            cout << "Yes!" << endl;
        }

    }
    
    return 0;
}

然后我们可以给要猜的数字一点随机性,利用Windows的时间来生成一个随机数字。

srand((unsigned int )time (NULL));

int a=rand()%100+1;

#include<iostream>
#include<cstdio>

using namespace std;
int main()
{
	

	
	
	int num2;
	srand((unsigned int)time(NULL));
	int num1 = rand() % 100 + 1;

	cout << "请猜出随机生成的数字(0~100)" << endl;
	start = clock();
	
	while (1)
	{
		cin >> num2;
		if (num2 > num1)
		{
			cout << "很遗憾,数字偏大" << endl;
		}
		else if (num2 < num1)
		{
			cout << "很遗憾,数字偏小" << endl;
		}
		else if (num2 = num1)
		{
			cout << "恭喜,游戏胜利" << endl;
			break;
		}
	}
	
	
	

		return 0;
}

这样一个可以玩起来的猜数字游戏就完成了。

 最后,可以通过Windows的记录时间的功能来调整难度(对猜数字的时间有所限制)

 

#include<iostream>
#include<cstdio>
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>//windows编程头文件



using namespace std;
int main()
{

	time_t start=0, end=0;
	int num2;
	srand((unsigned int)time(NULL));
	int num1 = rand() % 100 + 1;
	char level;
	cout << "请猜出随机生成的数字(0~100)" << endl << endl << endl << endl;
	cout << "请选择难度" << endl;
	cout << "A.困难(10s)  B.普通(15s)  C.简单(20s)  D.?????" << endl;
	cin >> level;
	
	start = clock();
	if (level == 'A')
	{
		cout << "请开始猜数字" << endl;
		while (1)
		{
			cin >> num2;
			if (num2 > num1)
			{
				cout << "很遗憾,数字偏大" << endl;
			}
			else if (num2 < num1)
			{
				cout << "很遗憾,数字偏小" << endl;
			}
			else if (num2 = num1)
			{
				end = clock();
				if ((end - start) / 1000 > 10)
				{
					cout << "超时,请再试一次" << endl<<"用时"<< (end - start) / 1000 << "s" << endl;
					break;
				}
				else
				{
					cout << "恭喜,游戏胜利" << endl;
					cout << "您共用时" << (end - start) / 1000 << "s" << endl;
					break;
				}
			}
			
		}
	}
	else if (level == 'B')
	{
		cout << "请开始猜数字" << endl;
		while (1)
		{
			cin >> num2;
			if (num2 > num1)
			{
				cout << "很遗憾,数字偏大" << endl;
			}
			else if (num2 < num1)
			{
				cout << "很遗憾,数字偏小" << endl;
			}
			else if (num2 = num1)
			{
				end = clock();
				if ((end - start) / 1000 > 15)
				{
					cout << "超时,请再试一次" << endl << "用时" << (end - start) / 1000 << "s" << endl;
					break;
				}
				else
				{
					cout << "恭喜,游戏胜利" << endl;
					cout << "您共用时" << (end - start) / 1000 << "s" << endl;
					break;
				}
			}

		}
	}
	else if (level == 'C')
	{
		cout << "请开始猜数字" << endl;
		while (1)
		{
			cin >> num2;
			if (num2 > num1)
			{
				cout << "很遗憾,数字偏大" << endl;
			}
			else if (num2 < num1)
			{
				cout << "很遗憾,数字偏小" << endl;
			}
			else if (num2 = num1)
			{
				end = clock();
				if ((end - start) / 1000 > 20)
				{
					cout << "超时,请再试一次" << endl << "用时" << (end - start) / 1000 << "s" << endl;
					break;
				}
				else
				{
					cout << "恭喜,游戏胜利" << endl;
					cout << "您共用时" << (end - start) / 1000 << "s" << endl;
					break;
				}
			}

		}
	}
	else if (level == 'D')
	{
		cout << "请开始猜数字" << endl;
		srand((unsigned int)time(NULL));
		int num3 = rand() % 100+1;
		if (num3 <= 10)
		{
			cout << "You only have " << num3 << "s " << "to finish it !!!!!" << endl;
		}
		else if(num3<=50)
		{
			cout << "You will have" << num3 << " s " << "to finish it." << endl;
		}
		else
		{
			cout << "You will have enough time to finish." << endl;
		}
		while (1)
		{
			cin >> num2;
			if (num2 > num1)
			{
				cout << "很遗憾,数字偏大" << endl;
			}
			else if (num2 < num1)
			{
				cout << "很遗憾,数字偏小" << endl;
			}
			else if (num2 = num1)
			{
				end = clock();
				if ((end - start) / 1000 > num3)
				{
					cout << "超时,请再试一次" << endl << "用时" << (end - start) / 1000 << "s" << endl;
					break;
				}
				else
				{
					cout << "恭喜,游戏胜利" << endl;
					cout << "您共用时" << (end - start) / 1000 << "s" << endl;
					break;
				}
			}

		}
	}
	else
	{
		cout << "What are you doing?" << endl << endl;
		system("Pause");
		cout << "It looks like you are bored with this game. " << endl;
		system("Pause");
		cout << "Let's try some new things." << endl;

	}




	return 0;
}

这样不同难度就区分出来了

甚至还可以利用随机的那段代码来实现随机时间限制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值