|
30鱼币
- #include <iostream>
- int main()
- {
- int target = 1, var = 1, count = 1;
- std::cout << "请输入若干个整数:" << std::endl;
- if (std::cin >> target) {
- while (std::cin >> var) {
- if (var == target)
- count += 1;
- else {
- std::cout << target << "出现了" << count << "次" << std::endl;
- target = var;
- count = 1;
- }
- }
- std::cout << target << "出现了" << count << "次" << std::endl;
- }
- return 0;
- }
复制代码
我有两个问题:
1. 我只输入一个非整数,如1.1,但还是会输出“1出现了1次”。为什么输入了非整型的数字(无效输入)依然会执行下方代码块的代码?(如输入字母a时就不执行if了)
2. 我输入“1.1 2 2 2”时,只会输出“1出现了1次”。为什么只执行下方代码块中的“std::cout << target << "出现了" << count << "次" << std::endl;”,却不将后续三个满足int类型的2执行while (std::cin >> var) 呢?
|
|