无法弄清楚为什么我的变量不断恢复为零

时间:2016-06-05 23:43:27

标签: c++

我正在为学校制作一个控制台骰子游戏项目。我遇到的一个问题是,当我试图让骰子得分时,得分变量总是恢复为零,而不是应该是什么。我可能只是完全忽视了一些简单的东西,因为我是C ++的新手,但也许你们都可以提供帮助。

以下是评分方法的代码:

void FindScoreNumbers()
{
    int scoreDie[6];
    int score = 0;
    bool isScoring = false;
    int flushCounter1 = 0;
    int flushCounter2 = 0;
    int flushCounter3 = 0;
    int flushCounter4 = 0;
    int flushCounter5 = 0;
    int flushCounter6 = 0;

    scoreDie[0] = castDie[0];
    scoreDie[1] = castDie[1];
    scoreDie[2] = castDie[2];
    scoreDie[3] = castDie[3];
    scoreDie[4] = castDie[4];
    scoreDie[5] = castDie[5];


    for (unsigned int i = 0; i < 6; ++i)
    {
        if (scoreDie[i] == 1)
        {
            score + 100;
            flushCounter1++;
            isScoring = true;

            if (flushCounter1 >= 3)
            {
                score + 800;
                isScoring = true;

            }
        }

        if (scoreDie[i] == 2)
        {
            flushCounter2++;
            if (flushCounter2 >= 3)
            {
                score + 200;
                isScoring = true;

            }
        }

        if (scoreDie[i] == 3)
        {
            flushCounter3++;
            if (flushCounter3 >= 3)
            {
                score + 300;
                isScoring = true;

            }
        }

        if (scoreDie[i] == 4)
        {
            flushCounter4++;
            if (flushCounter4 >= 3)
            {
                score + 300;
                isScoring = true;

            }
        }

        if (scoreDie[i] == 5)
        {
            score + 50;
            flushCounter5++;
            isScoring = true;

            if (flushCounter5 >= 3)
            {
                score + 400;
                isScoring = true;

            }
        }

        if (scoreDie[i] == 6)
        {
            flushCounter6++;
            if (flushCounter6 >= 3)
            {
                score + 600;
                isScoring = true;

            }
        }
    }

    if (isScoring = true)
    {
        std::cout << score << std::endl;    
    }
    else
    {
        std::cout << "FARKLE! You didn't roll any scoring die." << std::endl;
    }

}

如果格式不正确,我很抱歉这是我的第一个问题。顺便说一句,castDie[6]是来自不同代码的滚动骰子数组。我也知道它会返回分数

2 个答案:

答案 0 :(得分:2)

score + 800;应为score = score + 800;(所有其他人都一样)。

您也可以使用score += 800;

快捷方式

答案 1 :(得分:0)

应该是:

score += 100;

这是:

的简写
score = score + 100;