#include "stdafx.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){int a, b;do{while (1){cout << "Please input two integers :";cin >> a >> b;if ((int)a != a || (int)b != b){cout << "Aren't you input two integers,please try again" << endl;continue;}else break;}if (a > b)cout << "The larger number is " << a << endl;elsecout << "The larger number is " << b << endl;} while (a = b);return 0;}执行之后如果输入的不是整数就会重复输出Please input two integers 与The larger number is这是为啥
3 回答
倚天杖
TA贡献1828条经验 获得超3个赞
Cats萌萌
TA贡献1805条经验 获得超9个赞
注释的地方改一下就可以了,
| 12345678910111213141516171819202122232425262728 | #include <iostream>using namespace std;int main(){ double a, b;//定义改一下 do { while (1) { cout << "Please input two integers :"; cin >> a >> b; if ((int)a != a || (int)b != b) { cout << "Aren't you input two integers,please try again" << endl; continue; } else break; } if (a > b) cout << "The larger number is " << a << endl; else cout << "The larger number is " << b << endl; } while (a == b);//改成== system("pause"); return 0;} |
虽然是整数,但是你输入可能是浮点型的,所以用double类型的作为输入
Qyouu
TA贡献1786条经验 获得超11个赞
if ((int)a != a || (int)b != b)
看起来实际想要的是检查输入是否正确,那么可以用if (!cin)来判断输入流状态,然后用cin.clear()清除错误标记,cin.ignore(1024,'\n')丢弃错误的字符……
- 3 回答
- 0 关注
- 183 浏览
添加回答
举报
0/150
提交
取消

