C++ 骰子游戏问题,忽略if语句

C++ 骰子游戏问题,忽略if语句,c++,C++,我现在所在的班级希望我做一个掷骰子游戏 问题在于第二个while语句的int main中比较了点和滚动。它忽略if语句并再次执行循环,即使它到达点或7。有时它的工作原理与它应该的一样,而有时它会重复循环几次 #include "pch.h" #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int diceRoll() { int x = 0,

我现在所在的班级希望我做一个掷骰子游戏

问题在于第二个while语句的int main中比较了点和滚动。它忽略if语句并再次执行循环,即使它到达点或7。有时它的工作原理与它应该的一样,而有时它会重复循环几次

#include "pch.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int diceRoll() {

    int x = 0, y = 0;

    x = rand() % 6 + 1;
    y = rand() % 6 + 1;

    cout << "You rolled a " << x << " and a " << y << " which comes out to ------> " << x + y << " <-------" << endl;

    return x + y;
}

bool playAgain() {
    char ans;
    cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
    cin >> ans;

    while (ans != 'Y' || ans != 'y' || ans != 'n' || ans != 'N') {
        if (ans == 'Y' || ans == 'y')
        {
            return true;
        }
        if (ans == 'N' || ans == 'n')
        {
            return false;
        }       
        cout << "Do you want to play again ?? Y to continue, N to quit." << endl;
        cin >> ans;
    }
}


int main()
{
    srand(time(NULL));
    int dices, bid, point = 0;
    int money = 50;
    bool gameRunning = true;
    bool didTheyWin;

    while (gameRunning == true) {
        if (money == 0) {
            cout << "You have no money, ending game." << endl;
            break;
        }

        cout << "Please enter a bid. You currently have $" << money << endl;
        cout << "$";
        cin >> bid;

        while (bid > money) {
            cout << "Please bet below your current balance: $" << money << endl;
            cout << "$";
            cin >> bid;
        }

        dices = diceRoll();
        didTheyWin = false;
        if ((dices == 7) || (dices == 11)) {
            cout << "You won $" << bid << " !" << endl;
            money = money + bid;
        }
        else if ((dices == 2) || (dices == 3) || (dices == 12)) {
            cout << "You LOSE! You lost $" << bid << " !" << endl;
            money = money - bid;
        }
        else  {
            point = dices;
            cout << "The target number is > " << point << " <" << endl;
            cout << "If you hit a 7 you lose, if you hit the target you win. \nYou automatically roll until one of these two things happen.\n";
            while (didTheyWin == false) {
                diceRoll();
                dices = diceRoll();
                if (dices == point) {
                    cout << "You won $" << bid << " !" << endl;
                    money = money + bid;
                    cout << "You now have $" << money << endl;
                    didTheyWin = true;
                }
                else if (dices == 7) {
                    cout << "You LOSE! You lost $" << bid << " !" << endl;
                    money = money - bid;
                    cout << "You now have $" << money << endl;
                    didTheyWin = true;
                }
            }
        }
        gameRunning = playAgain();
    }

    cout << "Thanks for playing. _END_" << endl;

    return 0;
}
#包括“pch.h”
#包括
#包括
#包括
使用名称空间std;
int diceRoll(){
int x=0,y=0;
x=rand()%6+1;
y=rand()%6+1;

你可以调用两次骰子,忽略你从第一次调用中得到的结果。你会看到第一次掷骰的结果显示出来,但是它们会被忽略,你会再次掷骰。

请发布一个人们可以编译和测试自己的帖子。
ans!=“Y”| ans!=“Y”
总是正确的。对不起,第一次发布,我想我是uld只需发布一部分。你既不应该发布一部分,也不应该发布整个内容;你应该发布一个.testx3,你要做的是将你的程序减少到演示你需要帮助的错误所需的最低代码。这将生成一个大约5行长的完整程序。作为额外的奖励,一旦不必要的代码消失了,您可能会看到错误以及如何自己修复它,从而使问题变得毫无意义。如果您正在问一个问题,并且还没有生成MCVE或类似的东西,那么您问这个问题太早了,应该进行更多的调试。