我不断从代码中反复获得相同的结果

I keep repeatedly getting the same result from code

本文关键字:结果 代码      更新时间:2023-10-16
我想应该

像老虎机一样"旋转"水果并输出屏幕上的水果,出于某种原因,即使我的随机旋转整数确实发生了变化,它也只给了我苹果。我已经尝试逐步执行代码,但不确定我到底在寻找什么,出于某种原因,当我逐步执行时,它也给了我 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");
}

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

Apple 的值大于所有其他值。 然而,您首先要检查它。 只有两种可能的结果是苹果或什么都没有