繁体   English   中英

While循环不循环

[英]While loop doesn't loop

在这里,我建立了一个while循环,在其中输入选择数字1或2。如果选择1或2,则选择的数字将为1或2。 如果我选择一个小于1或大于2的数字,则转到else语句,该语句要求用户再次输入。 但是,如果用户输入了问题,那么如果他们选择正确的数字1或2,就不会循环回到开头。 它只会在那里结束循环,而不会进入正确的if子句。 为什么是这样? 我觉得好像攻击正确。

int menus (int selection)
{
    while ( selection > 2 ||  selection < 1)
    {
        if (selection == 1)
        {
            cout << " 1 " << endl;
        }
        if (selection == 2)
        {
        cout << " 2 " << endl;
        }
        else
            cout << "You didnt choose 2 or 3. Choose again";
        cin >> selection;
    }
    return 0;
}

您的循环条件被扭曲。

仅当选择为3、4、5,...(由于selection > 2 )或selection为0,-1,-2,...(由于selection < 1 )时,才经历循环。 因此, if 你去else 然后,您可以在重做循环之前无条件输入新选择。 如果现在输入有效值,则循环终止。 您可能还会注意到,您的消息与您希望用户提供的消息不匹配。

正确缩进后,您的代码与此类似:

int menus (int selection)
{
    while (selection > 2 || selection < 1)
    {
        if (selection == 1)
        {
            cout << " 1 " << endl;
        }
        else if (selection == 2)
        {
            cout << " 2 " << endl;
        }
        else
            cout << "You didn't choose 1 or 2. Choose again: ";
        cin >> selection;
    }
    return 0;
}

如果您确实希望用户选择1或2并返回他们的选择,那么您需要更多类似的东西:

int menus (int selection)
{
    while (selection > 2 || selection < 1)
    {
        cout << "You didn't choose 1 or 2. Choose again: ";
        cin >> selection;
    }
    if (selection == 1)
        cout << " 1 " << endl;
    else if (selection == 2)
        cout << " 2 " << endl;
    return selection;
}

仍然存在诸如“ EOF会发生什么?”之类的问题。 应该处理的。 当然,打印可以简化:

int menus (int selection)
{
    while (selection > 2 || selection < 1)
    {
        cout << "You didn't choose 1 or 2. Choose again: ";
        cin >> selection;
    }
    cout << " " << selection << " " << endl;
    return selection;
}

仔细看看您的“ while”条件:只有在“ selection”大于2或小于1时,它才会循环。然后检查您选择的是1还是2-您刚刚排除的内容。 循环中也没有输入命令。

while循环不包含您要在循环内测试的值!

while ( selection > 2 ||  selection < 1)
{
if (selection == 1)
{
    // Impossible!! selection isn't > 2 or < 1.
}

您可以改为:

while (1)
{
    switch (selection) {
        case 1:
        case 2:
            cout << " " << selection << " " << endl;
            return 0;
        default:
            cout << "You didnt choose 1 or 2. Choose again";
            cin >> selection;
    }
}

我不明白为什么选择是函数的参数。 没有更多的代码很难说这是否明智。

采用

do{

}while( selection > 2 ||  selection < 1);

而不是一会儿。

在我所看到的中,您正在将选择传递给函数。 仅当输入不是1或2时,while循环才会运行。

因此,将代码更改为:

int menus (int selection)
{
  while (true)
 {
  if (selection == 1 || selection == 2)
  {
     cout << selection << endl;
     break;
  }
  else
  {
     cout << "You didnt choose 1 or 2. Choose again";
     cin >> selection;
  }
 }

 return 0;
}

像这样在其他内部调用int menus()

else{
cout << "You didnt choose 2 or 3. Choose again";
cin >> selection;
menus(selection);
}

如果他们选择一个有效数字,则该数字将不再满足循环的条件要求。 您会发现,如果用户失败太多次,可以更容易地设置布尔值,否则,可以使用do{ /* get the input, count it or ask them to fix it */ }while(selection == 1 || selection == 2); 当数字变为无效时,它将停止循环播放

如果输入1或2,甚至不会进入循环,因为1或2都不大于2或小于1,它们相等。

这应该解决它:

while ( selection >= 2 ||  selection <= 1) //equals while(true)

请注意,您还应该在第二个if语句上使用else if语句,否则它将无法正常工作。

else if (selection == 2)
{
    cout << " 2 " << endl;
    break; // this leaves the loop
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM