我不断从代码中得到相同的结果

时间:2015-01-31 05:08:33

标签: c++

假设'旋转"水果就像老虎机我猜想并输出水果在屏幕上的东西,出于某种原因,即使我的randomSpin整数确实改变,它也只给我苹果。我已经尝试逐步完成代码,但我不确定我正在寻找什么,出于某种原因,它也给了我w1.spin,w2.spin和w3.spin作为相同的数字(对于randomSpin)当我踩过它时。

class Cspinner
{
private:

    int randomSpin;
    string FruitName;

    int apple;
    int orange;
    int cherry;
    int banana;
    int peach;

public:
    Cspinner()
{
    srand(time(NULL));
    apple = 30;
    orange = 25;
    cherry = 20;
    banana = 15;
    peach = 10;
}

    Cspinner(int newapple, int neworange, int newcherry, int newbanana, int newpeach)
    {
        randomSpin = 0;
        apple = newapple; //set apple to new value
        orange = neworange; //set orange to new value
        cherry = newcherry; //set cherry to new value
        banana = newbanana; //set banana to new value
        peach = newpeach; //set peach to new value

        srand(time(NULL));
    }


    void spin()
    {
        randomSpin = rand() % 100 + 1;

        if ((randomSpin >= 1) && (randomSpin <= apple))
        {
            FruitName = "apple ";
        }
        else if ((randomSpin > apple) && (randomSpin <= orange))
        {
            FruitName = "orange ";
        }
        else if ((randomSpin > orange) && (randomSpin <= cherry))
        {
            FruitName = "cherry ";
        }
        else if ((randomSpin > cherry) && (randomSpin <= banana))
        {
            FruitName = "banana ";
        }
        else if ((randomSpin > banana) && (randomSpin <= peach))
        {
            FruitName = "peach ";
        }
    }

    void show()
    {
        cout << FruitName;
    }

};


void main()
{
    Cspinner w1;
    Cspinner w2;
    Cspinner w3(80, 5, 5, 5, 5);
    for (int x = 0; x <= 9; x++)
    {
        w1.spin();
        w2.spin();
        w3.spin();
        w1.show();
        w2.show();
        w3.show();
        cout << endl;
    }
    system ("pause");
}

2 个答案:

答案 0 :(得分:3)

如果您认为这些整数是百分比,请将它们累积起来。对于橙子的截止,现在使用苹果和橙子的总和。对于樱桃,前三个(苹果,橙子和樱桃)的总和。如果您希望用户能够输入百分比,请在您用于设置阈值的函数中表达此过程(Cspinner构造函数)。

答案 1 :(得分:0)

apple的值大于所有其他值。但是你先检查它。只有两种可能的结果是苹果或没有什么