这个逻辑运算工作有什么问题?

What's wrong with this logical operation work?

本文关键字:什么 问题 工作 逻辑运算      更新时间:2023-10-16

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

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

对于所有roundBet,这将计算为true,因为一个数字要么不是100(roundBet != 100真的)要么不是100(不是300,roundBet != 300真的)

您需要的是:

if ((roundBet != 100) && (roundBet != 300) && (roundBet != 500))
其中一个

选择永远是正确的,因为如果roundBet是,比如说,100,那么它将不同于300500

使用逻辑 AND &&