无法在while循环之外获取int更新

时间:2016-11-08 06:58:21

标签: c++

当它运行时,银行会收取或增加奖金或损失,然后再次运行,银行将被设置为25美元的银行而非更新的银行

int main()
{
srand(time(0));
int bank = 25;
int total;

char answer;

cout << "Come play Spin the Wheel. The wheel has numbers from 1-10." <<  endl
    << "If you spin an even number you lose that amount. If you spin" << endl
    << "an odd number you win that amount. You start with a 25$ bank." << endl; 
cout << "Your bank is $" << bank << ". Would you like to spin the wheel? (y/n):" << endl;
cin >> answer;

while (toupper(answer) == 'Y')
{
    int num = rand() % 10 + 1;

    if (bank <= 10)
    {
        cout << "Sorry you must have more than 10$ to play" << endl;
    }
else if (num % 2 == 0 )
    {
    total = bank + num;
        cout << "You spun a " << num << " and won $" << num << endl;
        cout << "Your bank is now: $" << total << endl;
    }

    else

    {
        total = bank - num;
        cout << "You spun a " << num << " and lost $" << num << endl;
        cout << "Your bank is now: $" << total << endl;
    }
    cout << "Would you like to play Again (y/n) ?" << endl;
    cin >> answer;
}





return 0;
}

当它运行时,银行会收取或增加奖金或损失,然后再次运行,银行将被设置为25美元的银行而非更新的银行

3 个答案:

答案 0 :(得分:0)

您在此功能中将银行初始化为25美元。在while循环内只有total更新而不是bank。

当玩钱太少时会出现另一个问题。我想你想要一次显示你的消息,但是你被困在循环中因为它永远不会被打破。

答案 1 :(得分:0)

我相信你的其他if和else语句是倒退的。

num % 2 == 0 

如果属实,则表示该数字是偶数。指示说,如果数字是偶数,你会赔钱。

银行永远不会低于或等于10,因为在最初的25之后你永远不会将银行设置为任何东西。它始终是25。

变量总数似乎是多余的。也许加上或减去直接从银行滚动的金额。

答案 2 :(得分:0)

您需要设置

T

否则你永远不会改变它的价值