猜数字游戏
在1~100之间猜测数字,猜大或者猜小了,将提醒猜测者,猜错五次后提醒用户,猜错次数达到六次后不允许再猜了直接退出程序,猜对了直接提醒用户猜对了并退出程序。
#include<iostream>;
#include<ctime>;
using namespace std;
int main() {
srand((unsigned int)time(NULL));
int num1 = rand() % 100+1;
int num2 = 0;
int num3 = 0;
while (1) {
cout << "请输入你猜测的数值:" << endl;
cin >> num2;
num3++;
if (num3 == 5) {
cout << "你已经猜错五次了,再猜测错误将退出哦!" << endl;
}
if (num3 == 6) {
cout << "你已经猜错六次了,无法再猜测,请联系管理员。" << endl;
break;
}
if (num2 > num1) {
cout << "猜大了" << endl;
}
else if(num2<num1)
{
cout << "猜小了" << endl;
}
else
{
cout << "猜对了" << endl;
break;
}
}
system("pause");
return 0;
}