#include<iostream>
using namespace std;
int main()
{
cout << "================猜数字=====================" << endl;
cout << "游戏规则:输入0~100的整数,有五次机会\n" << endl;
//生成随机数
srand(time(0));
int target = rand() % 100;
//利用循环不断进行猜测
int n = 0;//猜的次数
while (n < 5) {
cout << "请输入0~100的整数:" << endl;
int num;
cin >> num;
if (num == target) {
cout << "恭喜你,猜对了!幸运数字是:" << endl;
break;
}
else if (num > target) {
cout << "数字太大了,再猜一遍" << endl;
}
else {
cout << "数字太小了,再猜一遍" << endl;
}
++n;
}
if (n == 5) {
cout << "已经猜过5遍还没有猜中,欢迎下次再来!" << endl;
}
cin.get();
cin.get();
}
猜数游戏案例
于 2024-02-17 15:56:32 首次发布
本文介绍了如何使用C++编写一个简单的猜数字游戏,用户有五次机会猜测0到100之间的随机数,游戏会给出提示直到猜中或机会用完。
摘要由CSDN通过智能技术生成