Sor*_*yEh -1 c++ if-statement operand
如何让编译器检查语句的左侧和右侧?如果我没有弄错的话,我认为在C语言中,如果你有&&或者它可以左右读取||......所以当我查看C++时,它只会检查左边是否为真......我需要的是能够检查双方是否都是真的.
所以:
//Transactions has been initialized to 0
1. if deposit OR withdraw are greater than or equal to 1, add 1 to variable transactions.
2. if deposit AND withdraw are BOTH greater than or equal 1, then add 2 to variable transactions.
3. else if BOTH are less than 1, transaction is 0.
if (deposit >= 1 || withdraw >=1)
{
transactions = transactions + 1;
cout << "Transactions: " << transactions << endl;
}
else if (deposit >= 1 && withdraw >=1)
{
transactions = transactions + 2;
cout << "Transactions: " << transactions << endl;
}
else
{
cout <<"Transactions: " << transactions << endl;
}
Run Code Online (Sandbox Code Playgroud)
我遇到的这个问题是,它只读取左侧,因此事务只返回1.
感谢您的时间!
编辑
&&首先是条件,然后是||条件else if.
一个解释,由天顶提供(如果这有助于你,则在评论中给他+1):
最具限制性的案例需要先行,因为如果
A && B是的话true,A || B总会true如此.因此如果你把&&后||,该||案件将赶上每当&&情况true.
另外,另一边注意:离开cout所有支架的外部,你可以删除else.无论如何都要打印,所以不需要输入3次.