【问题标题】:What's wrong with this logical operation work?这个逻辑运算工作有什么问题?
【发布时间】:2013-11-11 02:15:40
【问题描述】:

我正在尝试创建一个 if 语句来验证用户的赌注是 100、300 还是 500。我做错了什么?

if ((roundBet != 100) || (roundBet != 300) || (roundBet != 500))
{
    cout << "Incorrect input";
    // Call round again
    newRound();
}

【问题讨论】:

  • 请注意,您可以从 if 语句中删除四分之三的括号而不改变其含义。

标签: c++ logical-operators


【解决方案1】:
if ((roundBet != 100) || (roundBet != 300) || (roundBet != 500))

对于所有roundBet,这将评估为true,因为数字不是100(roundBet != 100 true)或100(不是300,roundBet != 300 true)

你需要的是:

if ((roundBet != 100) && (roundBet != 300) && (roundBet != 500))

【讨论】:

    【解决方案2】:

    其中一个选项总是正确的,因为如果roundBet100,那么它将不同于300500

    使用逻辑与&amp;&amp;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-07
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      相关资源
      最近更新 更多