本帖最后由 sundeam 于 2023-11-17 09:52 编辑
各位大佬,我是c++菜鸟,今天遇到一个循环判断的问题,一直找不到原因,求指教。
[C++] 纯文本查看 复制代码 #include<iostream>
#include <limits>
#include<random>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
int num = 0; int a = 0; int b = 100; int c = 0; time_t t;
srand((unsigned)time(&t));
num = rand()%100;
for (;;)
{
cout << "请输入" << a << "---" << b << "之间任意一个数值";
cin >> c;
if (num==c)
{
cout << "恭喜你答对了~!" << endl;
a = 0; b = 100;
num = rand() % 100;
}
if (num>c>a)
{
a = c;
}
if (b>c>num)
{
b = c;
}
}
每次循环到if (num>c>a) 可以正常判断,执行完一次后,后面输入的数据就不判断了?
经过大佬的分享,完美解决判断异常问题,谢谢~!
下面附上修改后的
using namespace std;
int main()
{
int num = 0; int a = 0; int b = 100; int c = 0; time_t t;
srand((unsigned)time(&t));
num = rand()%100;
for (;;)
{
cout << "请输入" << a << "---" << b << "之间任意一个数值";
cin >> c;
if (num==c)
{
cout << "恭喜你答对了~!" << endl;
a = 0; b = 100; c = 0;
num = rand() % 100;
}
if (c < 0 && c>100)
{
cout << "你输入的数值超出范围,请重新输入:" << endl;
c = 0;
}
if (num>c && c>a)
{
a = c;
}
if (b>c && c>num)
{
b = c;
}
} |