C++:猜数字

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
    
    
	// 系统生成随机数
	int key = rand() % 100 + 1;
	//用户输入的数字
	int num;
	//标志变量,猜对退出循环
	int flag = 0;

	while (flag == 0)
	{
    
    
		//用户输入数字
		cout << "请输入数字:" << endl;
		cin >> num;
		cout << num << endl;

		//判断数字的正确性
		if (num == key)
		{
    
    
			cout << "恭喜你,猜对了!" << endl;
			flag == 1;
		}
		else if (num < key)
		{
    
    
			cout << "猜小了" << endl;
		}
		else if (num > key)
		{
    
    
			cout << "猜大了" << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Alan_King79/article/details/124946120
今日推荐