首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在while循环中保存int值?

如何在while循环中保存int值?
EN

Stack Overflow用户
提问于 2016-10-27 05:40:18
回答 2查看 160关注 0票数 0

我的问题是,我如何在while循环中保存一个int值,我的代码都是关于赌博的,你从1,000开始,你想要赚到最多的现金,但当我再次滚动时,我的现金恢复到了我设置的原始值。

我的代码是这样的(注意,我是新手,所以不要嘲笑它有多糟糕)

代码语言:javascript
复制
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib> 
using namespace std;


int main()
{
    char again = 'Y';
    int test;
    int yes;
    int CashW;
    CashW = 1000;
    int CashL;
    CashL = 1000;
    int yLose;
    yLose = 500;
    int xCash;
    xCash = 1000;
    int xRan;
    srand(time(0));
    xRan = rand() % 100 + 1;
    cout << " Welcome to the Gambling Game!" << endl;
        cout << " If the number is above 50 I win!" << endl;
        cout << " If the number is below 50 you lose!" << endl;
        while (again == 'y' || again == 'Y')
        {
        cout << " The Number I Choose Is: " << xRan << endl;


        CashL = xCash - xCash - xCash;
        CashW = xCash + xCash;


        if (xRan < 50) {
            cout << " You win, rats!" << endl;
            cout << " The cash you started with was: " << xCash << endl;
            cout << " The cash you have now is: " << CashW << endl;
            cout << " Type 1 to play again, type 2 to close the game." << endl;
            cin >> yes;

        }

        if (xRan > 50) {
            cout << " I win, you lose!" << endl;
            cout << " The cash you started with was: " << xCash << endl;
            cout << " The cash you have now is: " << CashL << endl;
            cout << " Type 1 to play again, type 2 to close the game." << endl;
            cin >> yes;

        }

        if (yes == 1) {
            cout << " Cool, a gambling man! Time to make some cash" << endl;
        }

    }


}
EN

回答 2

Stack Overflow用户

发布于 2016-10-27 05:52:24

在您的代码中,当前根据游戏结果显示CashWCashL

不幸的是,您只打印出结果,而没有将其存储到xCash中。因此,在下一次迭代中,您将使用相同的xCash值重新开始!

只需在显示结果的行下添加xCash = CashW;xCash = CashL;,就可以很容易地解决这个问题。

票数 1
EN

Stack Overflow用户

发布于 2016-10-27 06:09:39

您永远不会使用每次赢/输的金额来更新xCash。您不会在每次循环迭代中生成新的随机数。而且您陷入了一个无尽的循环中,因为您从未更新循环变量again

试试更多像这样的东西:

代码语言:javascript
复制
#include <cmath>
#include <stdio.h>
#include <iostream>
#include <ctime>
#include <cstdlib> 

using namespace std;

int main()
{
    const int CashW = 1000;
    const int CashL = 1000;

    int xCash = 1000;
    int xRan;
    char answer;

    srand(time(0));

    cout << " Welcome to the Gambling Game!" << endl;
    cout << " If the number is above 50 I win!" << endl;
    cout << " If the number is below 50 you win!" << endl;

    do
    {
        xRan = rand() % 100 + 1;
        cout << " The Number I Choose Is: " << xRan << endl;

        if (xRan < 50) {
            cout << " You win, rats!" << endl;
            cout << " The cash you started with was: " << xCash << endl;
            xCash += CashW;
            cout << " The cash you have now is: " << xCash << endl;
        }

        else if (xRan > 50) {
            cout << " I win, you lose!" << endl;
            cout << " The cash you started with was: " << xCash << endl;
            xCash -= CashL;
            cout << " The cash you have now is: " << xCash << endl;
        }

        else {
            cout << " dang, a draw!" << endl;
        }

        cout << " play again? " << endl;
        cin >> answer;

        if ((answer != 'y') && (answer != 'Y')) {
            cout << " All done? Come back again another time!" << endl;
            break;
        }

        cout << " Cool, a gambling man! Time to make some cash" << endl;
    }
    while (true);

    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40272542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档