题目:电脑产生一个0到100之间的随机数字,要求用户来猜,电脑会提示用户猜大了还是小了,如果输入的不是数字,电脑会提醒你输入无效。
#include<iostream>
using namespace std;
int main()
{
int guess = rand() % 100 + 1;
int m;
for (int i = 1;i < 101;i++)
{
cout << "第"<<i<<"次猜,请输入你猜的数字:"<<endl;
cin >> m;
if (cin.good() == 0)
{
cout << "输入无效哦";
return 0;
}
if (m == guess)
{
cout << "恭喜你猜对了";
break;
}
else if (m > guess)
{
cout << "太大"<<endl;
}
else
{
cout << "太小"<<endl;
}
}
}