这个C ++代码彩票猜测游戏出了什么问题?

时间:2017-09-25 23:20:16

标签: procedural-programming

我正在制作一个简单的彩票游戏应用程序,其中生成0到10之间的三个随机数,如果用户以正确的顺序获得所有三个,则他们获得100万。如果他们得到一个,那么他们赢得10美元,如果他们得到所有三个但不按顺序,他们赢得一千,如果两个匹配,那么他们得到1000美元。如果他们没有得到,那么他们什么也得不到。

这是我的代码。

int main() {

cout << "Hello, this is the lottery! Three random numbers between 0 and 10 will be generated. Guess what they are and the order!" << endl;
char answer;
cout << "Do you want to play? (y or n): " << endl;
cin >> answer;
while (answer == 'y' || 'Y')

{
    srand((unsigned)time(NULL));
    int ran1 = rand() % 10;
    int ran2 = rand() % 10;
    int ran3 = rand() % 10;
    int guess1, guess2, guess3;
    cout << "Enter your first number guess: " << endl;
    cin >> guess1;
    cout << "Enter your second number guess: " << endl;
    cin >> guess2;
    cout << "Enter your third number guess: " << endl;
    cin >> guess3;
    if ((guess1 != ran1 || ran2 || ran3) && (guess2 != ran1 || ran2 || ran3) && (guess3 != ran1 || ran2 || ran3))
        cout << "You won no money. Sucks for you." << endl;
    else
        if ((guess1 == ran1 || ran2 || ran3) || (guess2 == ran1 || ran2 || ran3) || (guess3 == ran1 || ran2) || ran3)
            cout << "You won 10 dollars!" << endl;
        else
            if ((guess1 && guess2 == ran1 && ran2) || (guess1 && guess3 == ran1 && ran3) || (guess2 && guess3 == ran2 && ran3))
                cout << "You won 100 dollars!" << endl;
            else
                if ((guess1 == ran1 || ran2 || ran3) && (guess2 == ran1 || ran2 || ran3) && (guess3 == ran1 || ran2 || ran3))
                    cout << "You won 1 thousand dollars! good job!" << endl;
                else
                    if ((guess1 == ran1) && (guess2 == ran2) && (guess3 == ran3))
                        cout << "You won 1 million dollars! jackpot!" << endl;

    cout << "The numbers were " << ran1 << "," << ran2 << "," << ran3 << endl;
    cout << "Play again?(y or n): " << endl;
    cin >> answer;
    if (answer == 'y')
        continue;
    else
        break;

}



cout << "Game Over" << endl;
system("pause");
return 0;

}

当我运行此代码时,事情并没有正确决定。它只是说“你没有赢钱。为你吮吸”。 Idk什么错可能它的东西很简单,但有人可以帮忙吗?感谢。

2 个答案:

答案 0 :(得分:0)

if ((guess1 != ran1 || ran2 || ran3) && (guess2 != ran1 || ran2 || ran3) && (guess3 != ran1 || ran2 || ran3))

应该是

if ((guess1 != ran1 || guess1 != ran2 || guess1 != ran3) && (guess2 != ran1 || guess2 != ran2 || guess2 != ran3) && (guess3 != ran1 || guess3 != ran2 || guess3 != ran3))

答案 1 :(得分:0)

这不是条件的顺序,即使那里也存在缺陷,但是你编写条件的方式也是如此。

(guess1 == ran1 || ran2)不同于(guess1 == ran1 || guess1 == ran2)

这是因为在c ++中,0以外的数字的计算结果为true。这就是每次评估为真的原因。

例如,假设guess1 = 1,ran1 = 2,ran2 = 3,那么

(guess1 == ran1 || guess1 == ran2)将评估为false,但

(guess1 == ran1 || ran2)将评估为true。