猜数字游戏

//猜数字游戏:电脑产生随机数(1-100),判断猜大了还是猜小了
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Menu()//游戏菜单
{
	cout <<"***************************"<<endl;
	cout <<"**********猜数字***********"<<endl;
	cout <<"**********1.play***********"<<endl;
	cout <<"**********0.exit***********"<<endl;
	cout <<"***************************"<<endl;
}
void Game()//游戏
{
	int ret=rand()%100+1;
	int guess = 0;
	while (1)//可进行多把游戏
	{
		cout << "请猜数字:>";
		cin >> guess;
		if (guess < ret)
		{
			cout << "猜小了" << endl;
		}
		else if (guess > ret)
		{
			cout << "猜大了" << endl;
		}
		else
		{
			cout << "恭喜你,猜对了!" << endl;
			break;
		}
	}
}
int main()
{
	srand((unsigned int)time(NULL));//时间戳,srand整个工程只需要调用一次
	int input = 0;
	do
	{
		Menu();
		cout << "请选择:>";
		cin >> input;
		switch (input)
		{
		case 1:
			Game();
			break;
		case 0:
			cout << "退出游戏" << endl;
			break;
		default:
			cout << "选择错误,重新选择" << endl;
			break;
		}
	} while (input);
	return 0;
}

--->注意事项:

rand():

相关推荐

  1. LeetCode:数字游戏

    2024-04-29 02:02:03       48 阅读
  2. 数字游戏

    2024-04-29 02:02:03       37 阅读
  3. Linux数字游戏

    2024-04-29 02:02:03       24 阅读
  4. (c语言)数字游戏

    2024-04-29 02:02:03       63 阅读
  5. C语言数字游戏

    2024-04-29 02:02:03       59 阅读
  6. leetcode299--数字游戏

    2024-04-29 02:02:03       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-29 02:02:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-29 02:02:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-29 02:02:03       82 阅读
  4. Python语言-面向对象

    2024-04-29 02:02:03       91 阅读

热门阅读

  1. BIO NIO AIO有什么区别?

    2024-04-29 02:02:03       25 阅读
  2. android13 RK356X 预装第三方apk失败

    2024-04-29 02:02:03       33 阅读
  3. Dockerfile

    2024-04-29 02:02:03       32 阅读
  4. c++day5

    c++day5

    2024-04-29 02:02:03      32 阅读
  5. shell 局域网IP探活脚本

    2024-04-29 02:02:03       29 阅读
  6. langfuse使用零星记录

    2024-04-29 02:02:03       31 阅读
  7. UI图中的opacity效果和代码效果不一样

    2024-04-29 02:02:03       26 阅读
  8. 基于EBAZ4205矿板的图像处理:01简介

    2024-04-29 02:02:03       30 阅读
  9. 生成式人工智能AIGC技术的发展现状和未来趋势

    2024-04-29 02:02:03       36 阅读
  10. Face XY project

    2024-04-29 02:02:03       24 阅读