【问题标题】:while loop if and elsewhile 循环 if 和 else
【发布时间】:2023-03-22 00:16:01
【问题描述】:

代码的作用是询问用户他想要哪张卡,并根据选择的卡打印出一份声明。

如果输入了 1,2 和 3 以外的数字,我的目标是循环回到卡片选择功能。

还有一个 for 循环允许这个过程进行多次。

什么是最好的方法,我该怎么做?

int CardSelect() {

  cout << "Enter 1 for hearts" << endl;
  cout << " " << endl;
  cout << "Enter 2 for diamonds" << endl;
  cout << " " << endl;
  cout << "Enter 3 for joker" << endl;

  return 0;
};

int main() {
  for (int i = 1; i <= 5; i++) {
    CardSelect();
    int cardchoice;
    cin >> cardchoice;

    cardchoice = CardSelect();

    if (cardchoice == 1) {
      cout << "You got hearts" << endl;
      loop = false;
    } else if (cardchoice == 2) {
      cout << "You got diamonds" << endl;
      loop = false;
    } else if (cardchoice == 3) {
      cout << "You got joker" << endl;
      loop = false;
    } else {
      cout << "Invalid choice" << endl;
      cout << "Please ensure you type in the right numbers" << endl;
    }
  }
}

【问题讨论】:

  • CardSelect() 总是返回零,因此它不会像您尝试的那样做。
  • CardSelect() 直接擦除存储在cardchoice中的使用输入
  • 有什么建议吗?
  • 如果您想显示这些消息,只需使用 void 类型函数。如果您只想显示函数中的某些内容,则返回任何内容都没有意义。
  • @coder 在你的 main 中这些 loop = false 语句背后的逻辑是什么?首先没有名为循环的布尔/布尔变量,其次如果你想在输入后中断为什么要使用循环?

标签: c++ if-statement while-loop


【解决方案1】:

将 CardSelect() 的返回类型更改为 void,因为您只需在该函数中打印一些语句:

void CardSelect() 
{ // Your cout statements
}

在 main() 中调用它,并为您的 cardchoice 变量使用 switch case。

如果您想继续运行 switch 语句直到获得有效输入,请将所有内容放入 inifinte 循环(例如 while(1))并通过将布尔值设置为 true 来设置退出条件(将其设置为 @987654326 @最初)并在满足条件时使用break来退出循环:

int main() 
{
  while(1)
  {
    bool valid = false;
    CardSelect(); // call to your function
    int cardchoice;
    cin >> cardchoice;

    switch(cardchoice)
    {
      case 1:      
      cout << "You got hearts" << endl;
      valid = true;
      break;

      case 2:     
      cout << "You got diamonds" << endl;
      valid = true;
      break;

      case 3:    
      cout << "You got joker" << endl;
      valid = true;
      break;

      default:
      cout << "Invalid choice" << endl;
      cout << "Please ensure you type in the right numbers" << endl;
      break;
    } if(valid) break;
  }
}

【讨论】:

  • 这是一个很好的例子,但我面临的一个问题是 for 循环继续进行。这似乎不起作用
  • 如果您没有任何后续代码要运行,那么只需在default: 范围内使用exit(0);
  • 感谢您的帮助,但如果输入错误的数字,它不会真正循环
  • @coder 我不明白你想要什么。对于cardchoice 的不同输入(可能是有效的 1、2、3 或错误输入),您是要继续循环直到 5 次,还是要在第一个错误数字处退出 for 循环,以及不再继续 for 循环?
  • 感谢您的耐心等待,它按我想要的方式工作。我只需要了解更多关于这个主题的信息。再次感谢:)
【解决方案2】:

首先,您要查找的是continue,其次您需要摆脱这条毫无意义的行: cardchoice = CardSelect(); 因为它会删除用户输入

int CardSelect() {
    cout << "Enter 1 for hearts" << endl;
    cout << " " << endl;
    cout << "Enter 2 for diamonds" << endl;
    cout << " " << endl;
    cout << "Enter 3 for joker" << endl;

    return 0;


};



int main() {

for (int i = 1; i <= 5; i++) {
    CardSelect();
    int cardchoice;
    cin >> cardchoice;


    if (cardchoice == 1) {
        cout << "You got hearts" << endl;
    }

    else if (cardchoice == 2) {
        cout << "You got diamonds" << endl;

    }

    else if (cardchoice == 3) {
        cout << "You got joker" << endl;
    }

    else {
        cout << "Invalid choice" << endl;
        cout << "Please ensure you type in the right numbers" << endl;
    }
}

}

【讨论】:

  • continue 在这里应该有什么目的?至少对于给定的代码,如果continue 存在与否,结果不会有差异。
  • @t.niese 是的,你是对的。我正在考虑删除它,但它不会增加代码的可读性吗?不确定编辑:我认为它没有
  • 不,如果您阅读continue,则不会,那么您假设后面有一些不应执行的代码,但如果continue 是,则没有不会执行的代码缺少,所以有continue 会导致假设代码中缺少某些东西。
【解决方案3】:

你不应该打电话给cardchoice = CardSelect();。

此调用正在用 0 覆盖 cardchoice。删除此调用。

您打印值以查看发生了什么。是一种很好的学习方式。 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2012-06-18
    • 1970-01-01
    • 2015-11-08
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多