1-100猜数字游戏
#include<iostream>
using namespace std;
void myGuss()
{
int num = rand() % 100 + 1;
int myNum = 0;
int count = 0;
bool flag = true;
cout << "亲输入你的猜测(只能是1~100)" << "";
while (flag)
{
cin >> myNum;
if (myNum >= 0 && myNum <= 100)
{
if (myNum > num)
{
cout << "你猜大了,再猜" << endl;
count++;
flag = true;
}
else if (myNum < num)
{
cout << "你猜小了,再猜" << endl;
count++;
flag = true;
}
else
{
count++;
cout << "你猜对了,恭喜" << endl;
flag = false;
}
if (count > 4)
{
cout << "抱歉,你猜的超过了次数" << endl;
flag = false;
}
if (flag == false)
{
break;
}
}
else
{
cout << "你输入数字错误,请重新输入" << endl;
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
cout << "***********************" << endl;
cout << "******1.开始游戏********" << endl;
cout << "******2.退出游戏********" << endl;
cout << "***********************" << endl;
int choose;
while (true)
{
cout << "请输入你的选择" << endl;
cout << "**1.开始游戏**" << endl;
cout << "**0.退出游戏**" << endl;
cin >> choose;
switch (choose)
{
case 1:
myGuss();
break;
case 0:
cout << "退出成功!" << endl;
exit(0);
default:
break;
}
}
}