在if语句中使用在if语句中使用的变量

时间:2019-10-23 06:13:45

标签: c++

我编辑了这篇文章以澄清我的问题。尝试运行以下程序。它总是卡在cin上。您可以根据需要分配任意数量的cin,但不会继续。感谢所有答案


int main(){

int eingabe;
int zweieuro = 2;

cin >> eingabe;


if(eingabe < 2)
{
  eingabe = eingabe - zweieuro;
  cout << eingabe;
}
}
```

3 个答案:

答案 0 :(得分:2)

问题在这里:

cit_work= element.find('citedWork').text
except:
   cit_work= None

如果输入为2,则while(input != 0) { if(input > two_euro) { input = input - two_euro; 循环将继续进行,但是永远不会输入while。您专门检查输入是否大于2,更大,所以您永远不会将输入完全减小为0。

我们来看一个例子:

if

假设您通过将input is 4 while (input != 0) => true, enter the loop if (input > 2) => true, enter the reduction step input = input-2 => input is 2 while (input != 0) => true, continue the loop if (input > 2) => false, skip the reduction step while (input != 0) => true, continue the loop if (input > 2) => false, skip the reduction step ... continue forever 更改为>来解决上述问题。如果您输入的不是偶数,将会发生什么?不是整数?也许您的>=条件不应该完全检查while,而是检查输入是否大于0

您还需要将减少步长更改为始终在输入大于0时进行,否则您将陷入无限循环中,输入为1(或0到2之间的任何值)。 / p>

答案 1 :(得分:0)

通过浮点计算,一些舍入可以防止结果被 准确。
例如,如果您累积十次0.1,然后测试此结果 恰好是1.0,您会惊讶地发现事实并非如此!
(在我的计算机上,我收到了1.1102230246251565e-16的错误)

因此,将input的某些更改结果与确切的0进行比较 可能不正确。
也许您应该考虑误差幅度。

答案 2 :(得分:0)

您需要更改您的while条件,如果您也要更改。 做一些测试/调试...检查例如等于2、5 ...的输入...