【问题标题】:Why does my srand return all outcomes?为什么我的 srand 返回所有结果?
【发布时间】:2017-01-08 17:06:27
【问题描述】:

这是一个简单的文字游戏的开始。在游戏中,你应该四处寻找地牢和收集文物。我用srand(time(0))来做一些事情,比如找什么阶段,攻击,你找到什么东西,我在编程方面还没有走多远,但我已经遇到了一个问题。我的 rand() 返回所有结果。当我运行游戏时(这不是完整的代码,顺便说一句),它会返回“你进入了地牢!”、“哦不,敌人来了!”和“你找到了神器!

void mainScreen()
{
    srand(time(0));
    cout << "Health: \n";
    cout << health;
    cout << endl;
    _sleep(500);
    cout << "Inventory: \n";
    cout << inventory;
    cout << endl;
    _sleep(500);
    cout << "Gold: \n";
    cout << gold;
    cout << endl;
    _sleep(500);
    cout << "Artifacts: \n";
    cout << artifacts;
    cout << endl;
    _sleep(500);
    cout << "Rolling the dice of fate... \n";
    int diceRoll = 1 + (rand() % 10);
    if (diceRoll = 1, 2, 3, 4, 5, 6) {
        cout << "You entered a dungeon! \n";
    }
    if (diceRoll = 7, 8) {
        cout << "Oh No! An enemy has arrived! \n";
    }
    if (diceRoll = 9, 10) {
        cout << "You found an artifact! \n";
    }
}

【问题讨论】:

  • 返回所有结果是什么意思?另外,== 用于比较,||逻辑或
  • = 和 , 不要做你认为他们做的事。尝试咨询有关 C++ 的参考资料,而不是猜测...
  • diceRoll = 1, 2, 3, 4, 5, 6 不是您检查多个值的方式
  • 那我该怎么做呢?

标签: c++ random srand


【解决方案1】:

这个:

if (diceRoll = 1, 2, 3, 4, 5, 6) {
    cout << "You entered a dungeon! \n";
}
if (diceRoll = 7, 8) {
    cout << "Oh No! An enemy has arrived! \n";
}
if (diceRoll = 9, 10) {
    cout << "You found an artifact! \n";
}

完全错误,你需要单独检查每个元素,像这样:

if (diceRoll == 1 || diceRoll ==2 || diceRoll == 3 || diceRoll == 4 diceRoll == 5 || diceRoll == 6) {
    cout << "You entered a dungeon! \n";
}
if (diceRoll == 7|| diceRoll == 8) {
    cout << "Oh No! An enemy has arrived! \n";
}
if (diceRoll == 9 ||diceRoll == 10) {
    cout << "You found an artifact! \n";
}

为了进一步简化第一个分支,您可以这样做:

if (diceRoll >= 1 || diceRoll <= 6) {
    cout << "You entered a dungeon! \n";
}

【讨论】:

    【解决方案2】:

    您的if 声明并没有按照您的想法行事。

    首先,当您应该使用 == 比较运算符时,您正在使用 = 赋值运算符。

    其次,您正在使用, 运算符,它计算左右表达式,然后返回右侧表达式的结果。

    所以,这段代码:

    if (diceRoll = 1, 2, 3, 4, 5, 6)
    {
        ...
    }
    if (diceRoll = 7, 8)
    {
        ...
    }
    if (diceRoll = 9, 10)
    {
        ...
    }
    

    实际上是在做同样的事情,这不是你想要的:

    diceRoll = 1;
    if (6)
    {
        ...
    }
    diceRoll = 7;
    if (8)
    {
        ...
    }
    diceRoll = 9;
    if (10)
    {
        ...
    }
    

    您需要这样做:

    if ((diceRoll == 1) ||
        (diceRoll == 2) ||
        (diceRoll == 3) ||
        (diceRoll == 4) ||
        (diceRoll == 5) ||
        (diceRoll == 6))
    {
        cout << "You entered a dungeon! \n";
    }
    else if ((diceRoll == 7) ||
            (diceRoll == 8))
    {
        cout << "Oh No! An enemy has arrived! \n";
    }
    else
    {
        cout << "You found an artifact! \n";
    }
    

    这可以使用范围比较来简化:

    if ((diceRoll >= 1) && (diceRoll <= 6))
    {
        cout << "You entered a dungeon! \n";
    }
    else if ((diceRoll >= 7) && (diceRoll <= 8))
    {
        cout << "Oh No! An enemy has arrived! \n";
    }
    else
    {
        cout << "You found an artifact! \n";
    }
    

    或替换为单个switch 语句:

    switch (diceRoll)
    {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        {
            cout << "You entered a dungeon! \n";
            break;
        }
    
        case 7:
        case 8:
        {
            cout << "Oh No! An enemy has arrived! \n";
            break;
        }
    
        case 9:
        case 10:
        {
            cout << "You found an artifact! \n";
            break;
        }
    }
    

    另外,附带说明一下,您不应该在每次调用mainScreen() 时都调用srand()(我假设在程序的生命周期中可以多次调用它)。 srand() 应该只被调用一次,所以你应该在调用mainScreen() 之前调用它在main()。

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 1970-01-01
      • 2018-01-24
      • 2015-03-09
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多