本文介绍了做问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以为while循环设置一个或其他类型的条件吗?

请参阅下面的非工作代码...

do

{

cout<< 您是会员还是非会员?输入'M或N:

" ;;

cin> charMstat;

charMstat = toupper(charMstat );

} while(charMstat!=''M''|| charMstat!=''N'');

谢谢

Can you have one or the other type conditions for a do while loop?
See the non-working code below...
do
{
cout << "Are you a Member or Non-member? Enter ''M'' or ''N'':
";
cin >charMstat;
charMstat = toupper(charMstat);
} while(charMstat != ''M'' || charMstat != ''N'');
Thanks

推荐答案



是的。

Yes.





[snip]

[snip]



这个同时条件永远是真的。如果charMstat是M并且

条件的第一部分是假的,然后charMstat不是N,所以

条件的第二部分将是真的,并且反之亦然。仅当charMstat等于M时,此

条件才为假。 _and_

等于N,这显然永远不会发生。


你需要再次思考你的逻辑。在什么情况下你想让你的循环退出?

This "while" condition will always be true. if charMstat is "M" and
the first part of the condition is false, then charMstat isn''t "N", and so
the second part of the condition will be true, and vice versa. This
condition would be false only if charMstat was equal to "M" _and_ it was
equal to "N", which obviously can never happen.

You need to think through your logic again. Under what circumstances do
you want your loop to quit?


这篇关于做问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:36