While循环不断重复

发布于 2024-12-05 18:04:24 字数 386 浏览 1 评论 0原文

我设置了一个 while 循环,我想选择 r 或 h,我不想使用 for 循环,但我想使用开关,为什么当我输入 r 或 h 时,它会不断重复这种情况下的 cout 一百万次?我不能只说一次..

while (chooseMove == 'r' or 'h')
{
    switch (chooseMove) 
    {
    case 'r':
            cout << "you chose r";

        break;
    case 'h':
        cout << "you chose h";
        break;
    }




} 

我也用forloops尝试过,并且遇到了同样的问题,我无法弄清楚

I setup a while loop where i want to choose r or h, i dont want to use forloops but i want to use a switch why is it when i enter r or h it keeps repeating a million times the cout for that case? I cant get it to just say it once..

while (chooseMove == 'r' or 'h')
{
    switch (chooseMove) 
    {
    case 'r':
            cout << "you chose r";

        break;
    case 'h':
        cout << "you chose h";
        break;
    }




} 

I also tried it with forloops and had the same problem i cant figure it out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泅渡 2024-12-12 18:04:24

您的意思是 while (chooseMove == 'r' 或 choiceMove == 'h')。您当前编写的内容相当于 ((chooseMove == 'r') or ('h')),并且 'h' 的计算结果为 true


也许您还在寻求有关输入逻辑的帮助:

char c;
bool success = false;

while (std::in >> c)
{
  switch(c) {
    case 'r': /* ... */ success = true; break;
    case 'h': /* ... */ success = true; break;
  }

  if (success) break;
}

如果输入流关闭,这也会终止,您可以使用 success 检查操作是否成功。

What you mean is while (chooseMove == 'r' or chooseMove == 'h'). What you've currently written is equivalent to ((chooseMove == 'r') or ('h')), and 'h' evaluates as true.


Maybe you were also asking for help with the input logic:

char c;
bool success = false;

while (std::in >> c)
{
  switch(c) {
    case 'r': /* ... */ success = true; break;
    case 'h': /* ... */ success = true; break;
  }

  if (success) break;
}

This will also terminate if the input stream is closed, and you can use success to inspect the success of the operation.

心的位置 2024-12-12 18:04:24

因为这就是你编程它要做的事情。

如果您希望循环停止或暂停(例如等待输入),您应该将该代码添加到循环中。

while (chooseMove == 'r' or chooseMove == 'h')
{
    switch (chooseMove) 
    {
    case 'r':
            cout << "you chose r";

        break;
    case 'h':
        cout << "you chose h";
        break;
    }
    std::cin >> chooseMove;  //stop and wait for more input
}

Because that's what you programmed it to do.

If you want the loop to stop or pause (and say, wait for input), you should add that code into the loop.

while (chooseMove == 'r' or chooseMove == 'h')
{
    switch (chooseMove) 
    {
    case 'r':
            cout << "you chose r";

        break;
    case 'h':
        cout << "you chose h";
        break;
    }
    std::cin >> chooseMove;  //stop and wait for more input
}
↙厌世 2024-12-12 18:04:24

这是一个问题:

while (chooseMove == 'r' or 'h')

试试这个:

while ((chooseMove == 'r') || (chooseMove == 'h'))

当你写的时候(它是如何编译的?or不是C++):

chooseMove == 'r' or 'h'

它被解释为:

(chooseMove == 'r') or ('h')

语句'h'始终为 true,因此 while 循环将永远运行。

This is a problem:

while (chooseMove == 'r' or 'h')

Try this instead:

while ((chooseMove == 'r') || (chooseMove == 'h'))

When you write (how did this even compile? or isn't C++):

chooseMove == 'r' or 'h'

It is interpreted as:

(chooseMove == 'r') or ('h')

The statement 'h' is always true, so the while loop runs forever.

酒解孤独 2024-12-12 18:04:24

chooseMove == 'r' 或 'h' 是什么意思?根据C++
标准,这被分组为 (chooseMove == 'r') 或 ('h');这
'h' 隐式转换为 bool 会产生 (chooseMove ==
'r')或('h'!= 0)
。第二个条件永远为真。

What does chooseMove == 'r' or 'h' mean? According to the C++
standard, this is grouped as (chooseMove == 'r') or ('h'); the
implicit conversion of 'h' to bool then results in (chooseMove ==
'r') or ('h' != 0)
. The second condition will always be true.

夏夜暖风 2024-12-12 18:04:24
while (chooseMove == 'r' or 'h')

这相当于:

while ( (chooseMove == 'r')  or true)
//same as while ( (chooseMove == 'r')  || true)

所以这是无限循环。请注意,or|| 是同一件事。

你想要的是这样的:

while ( (chooseMove == 'r')  or (chooseMove == 'h'))
//same as while ( (chooseMove == 'r')  || (chooseMove == 'h'))
while (chooseMove == 'r' or 'h')

which is equivalent to this:

while ( (chooseMove == 'r')  or true)
//same as while ( (chooseMove == 'r')  || true)

So this is infinite loop. Note that or and || are same thing.

What you want is this:

while ( (chooseMove == 'r')  or (chooseMove == 'h'))
//same as while ( (chooseMove == 'r')  || (chooseMove == 'h'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文