猜数字游戏(c++)

前面我也发布过纯C的猜数字游戏

有兴趣的小伙伴也可以看一下

最简单的猜数字游戏(纯C)_Koi279的博客-CSDN博客

如果大家对违法输入(比如输入特殊字符和字母)不明白的,我也有相关文章讲解

有关c\c++违规输入相关问题_Koi279的博客-CSDN博客

先看一下效果

这里我设置了四个难度

分别为

1~100

1~1000

1~10000

1~100000

的随机数。

vs下可执行代码如下

#include<iostream>
using namespace std;
void meau1()
{
	cout << "\t\t******************" << endl;
	cout << "\t\t**1.play  0.exit**" << endl;
	cout << "\t\t******************" << endl;
}
void meau2()
{
	cout << "\t 1.简单";
	cout << "\t 2.一般";
	cout << "\t 3.困难";
	cout << "\t 4.地狱" << endl;
}
void game(int a)
{
	cout << "\t\t请输入一个1~" << a << "的数字" << endl;
	srand((unsigned)time(NULL));
	int ret = rand() % a + 1;
	int n; while (1)
	{
		cin >> n;
		if (n > ret)
			cout << "\t猜大了!" << endl;
		else if (n < ret)
			cout << "\t猜小了!" << endl;
		else
		{
			cout << "\t恭喜你,猜对了!" << endl; break;
		}
	}
}
int main()
{
	cout << "\t\t    猜数字游戏" << endl;
	meau1(); int c1, c2;
	do
	{
		cout << "\n\t\t请选择:";
		back1:
		cin >> c1;
		if ((int)c1 != 1 && (int)c1 != 0)
		{
			cout << "选择错误,请按指示选择!" << endl;
			goto back1;
		}
		switch (c1)
		{
		case 1:
			cout << "\t\t请选择游戏难度:\n" << endl;
			meau2();
			back2:
			cin >> c2;			
			switch (c2)
			{
			case 1: game(100); break;
			case 2: game(1000); break;
			case 3: game(10000); break;
			case 4: game(100000); break;
			default:cout << "\t\t选择错误,请按指示选择!" << endl; goto back2;
			}
			break;

		case 0:
			cout << "退出游戏" << endl; break;
		}

	} while (c1);
	return 0;
}

DEV-C++下 可执行代码如下:

#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
void meau1()
{
	cout << "\t\t******************" << endl;
	cout << "\t\t**1.play  0.exit**" << endl;
	cout << "\t\t******************" << endl;
}
void meau2()
{
	cout << "\t 1.简单";
	cout << "\t 2.一般";
	cout << "\t 3.困难";
	cout << "\t 4.地狱" << endl;
}
void game(int a)
{
	cout << "\t\t请输入一个1~" << a << "的数字" << endl;
	srand((unsigned)time(NULL));
	int ret = rand() % a + 1;
	int n; while (1)
	{
		cin >> n;
		if (n > ret)
			cout << "\t猜大了!" << endl;
		else if (n < ret)
			cout << "\t猜小了!" << endl;
		else
		{
			cout << "\t恭喜你,猜对了!" << endl; break;
		}
	}
}
int main()
{
	cout << "\t\t    猜数字游戏" << endl;
	meau1(); int c1, c2;
	do
	{
		cout << "\n\t\t请选择:";
		back1:
		cin >> c1;
		if ((int)c1 != 1 && (int)c1 != 0)
		{
			cout << "选择错误,请按指示选择!" << endl;
			goto back1;
		}
		switch (c1)
		{
		case 1:
			cout << "\t\t请选择游戏难度:\n" << endl;
			meau2();
			back2:
			cin >> c2;			
			switch (c2)
			{
			case 1: game(100); break;
			case 2: game(1000); break;
			case 3: game(10000); break;
			case 4: game(100000); break;
			default:cout << "\t\t选择错误,请按指示选择!" << endl; goto back2;
			}
			break;

		case 0:
			cout << "退出游戏" << endl; break;
		}

	} while (c1);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Koi279

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

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

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

打赏作者

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

抵扣说明:

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

余额充值