如果/else即使在while循环内条件似乎已满足,但仍不执行
If/else if not executing even though it appears conditions are met inside of a while loop
差不多就是标题所说的。我已经完成了这个彩票号码分配的最后一部分,我不知道为什么在调试时没有显示第二组if/else-if语句。我知道if/else语句是互斥的,但一个if和另一个if不应该同时测试吗?这是代码。
count=0;
while(count<5)
{
if(lottery[count] == user[count])
{
lotto = lottery[count];
cout<<"So, you matched numbers with "<<lotto <<".n";
tally++;
}
if(tally==5 && count == 5)
{
cout<<"Congratulations, you're the grand prize winner!";
}
else if(tally < 5 && count == 5)
{
cout<<"A total of "<<tally<<" of your lottery picks matched.";
}
else if(tally == 0 && count == 5)
{
cout<<"Caution. The following comment is an inside joke. Read at your own risk.";
cout<<"Bet you feel like a total loser huh? Nothing matched.";
}
count++;
}
我知道为了简单起见,我可能应该用for循环代替while循环,但我对while更满意。
if
块时,count
将永远不会为5
。
一旦它变成5
,条件就会失败,循环就会停止。
count
在while循环中的if-else
条件需要的点处永远不会等于5
。如果在if-else
之前增加了count
,则可以满足其中一个条件(取决于tally
的值(。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- C++17中的并行执行策略
- QML按钮点击功能执行顺序
- 程序在执行程序的其余部分之前退出
- 为什么catch中的代码没有被执行
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 将执行、作业和WinAPI相乘
- Craps游戏在循环不满足退出条件时执行
- 如何防止 if 语句在接收满足语句的多个输入时执行
- 如果/else即使在while循环内条件似乎已满足,但仍不执行