【问题标题】:I cannot figure out why this while loop is endless [duplicate]我无法弄清楚为什么这个while循环是无止境的[重复]
【发布时间】:2013-11-12 05:06:12
【问题描述】:

这是我目前正在使用的代码:

bool playCraps(int currentGame, bool detailPrint, char isBetting, int startingBet)
{
    bool crapsResult = NULL;
    int currentGameStorage[100];
    int currentRoll = 1;
    int point = roll2Dice();
    int printingNumber = 0;
    currentGameStorage[0] = point;
    if(point == 7 || point == 11)
    {
        crapsResult = true;
    }
    else if(point == 2 || point == 3 || point == 12)
    {
        crapsResult = false;
    }
    else
    {
        crapsResult = NULL;
    }
    while(crapsResult != true || crapsResult != false)
    {
        currentGameStorage[currentRoll] = roll2Dice();
        if(currentGameStorage[currentRoll] == point)
        {
            crapsResult = true;
        }
        else if(currentGameStorage[currentRoll] == 7)
        {
            crapsResult = false;
        }
        currentRoll += 1;
    }
    currentRoll -= 1;
    if(detailPrint == true)
    {
        cout << "Game " << currentGame << ": ";
        for(printingNumber = 0; printingNumber <= currentRoll; printingNumber += 1)
        {
            cout << currentGameStorage[printingNumber] << " ";
        }
        if(crapsResult == true)
        {
            cout << "win";
        }
        else if(crapsResult == false)
        {
            cout << "lose";
        }
        cout << endl;
    }
    return crapsResult;
}

每当我运行它时,它都会创建一个无文本的循环,终端中不会出现任何文本。函数 roll2Dice() 使用 rand() 函数模拟两个六面骰子的掷骰,并将两个结果相加。任何帮助将不胜感激。

【问题讨论】:

  • 想想while(crapsResult != true || crapsResult != false)是什么意思,
  • 我将其设置为 and 循环,但现在我的输出出现问题。它应该输出数组中的每个值,但现在它输出一个长负数,而不是 1 到 12 之间的数字列表

标签: c++ function loops while-loop


【解决方案1】:

您的 while 测试 crapsResult != true || crapsResult != false 必然为真,因此不会终止。 crapsResult 值是真或假,这将使表达式的两半之一为真,另一半为假。 true || falsefalse || true 都计算为 true

【讨论】:

    【解决方案2】:

    正如其他人所指出的,循环永远不会结束的原因很明显。

    您对 NULL 的使用让我觉得您希望您可以拥有一个可以保存三个值(未设置、真和假)的布尔值。您可以通过使用指向 bool 的指针以最小的更改来实现这一点,但这非常令人作呕。枚举是你真正需要的:

    enum CrapsResult
    {
        unrolled,
        true_result,
        false_result
    };
    

    相关代码则变为*:

    CrapsResult crapsResult = unrolled;
    int currentGameStorage[100];
    int currentRoll = 1;
    int point = roll2Dice();
    int printingNumber = 0;
    currentGameStorage[0] = point;
    if(point == 7 || point == 11)
    {
        crapsResult = true_result;
    }
    else if(point == 2 || point == 3 || point == 12)
    {
        crapsResult = false_result;
    }
    else
    {
        crapsResult = unrolled;
    }
    
    while(crapsResult == unrolled)
    {
        currentGameStorage[currentRoll] = roll2Dice();
        if(currentGameStorage[currentRoll] == point)
        {
            crapsResult = true_result;
        }
        else if(currentGameStorage[currentRoll] == 7)
        {
            crapsResult = false_result;
        }
        currentRoll += 1;
    }
    

    *我还没有真正编译过这个。

    【讨论】:

      【解决方案3】:
      while(crapsResult != true || crapsResult != false)
      

      等于

      while(crapsResult == false || crapsResult == true)
      

      .

      //while(crapsResult == (false || true)) // Don´t matter
      

      所以 boolean 只有 false 或 true

      它总是会计算 true 并且等于这个表达式

      while(true)
      

      【讨论】:

        【解决方案4】:

        在 C 和 C++ 中,表达式可以根据以下条件解析为真或假:0 是 false,其他都是 true。 C的宏FALSE和TRUE的正式定义是#define FALSE 0#define TRUE (!(FALSE))

        bool 类型的变量可以是truefalse。您使用 NULL 值初始化 crapsResult。 NULL 是定义为0UL0ULL 的宏,其计算结果为false。

        bool crapsResult = NULL;
        

        等价于

        bool crapsResult = false;
        

        所以当您的代码执行以下操作时:

        while (crapsResult != true || crapsResult != false)
        

        总是满足其中一个条件,所以循环总是重复。

        您将需要第二个变量,大概是 bool 类型,以确定您是否有一个可接受的答案。

        bool crapsResult = false;
        bool haveCrapsResult = false;
        int currentGameStorage[100];
        int currentRoll = 1;
        int point = roll2Dice();
        int printingNumber = 0;
        currentGameStorage[0] = point;
        if(point == 7 || point == 11)
        {
            crapsResult = true;
            haveCrapsResult = true;
        }
        else if(point == 2 || point == 3 || point == 12)
        {
            crapsResult = false;
            haveCrapsResult = false;
        }
        
        while(!haveCrapsResult)
        {
            currentGameStorage[currentRoll] = roll2Dice();
            if(currentGameStorage[currentRoll] == point)
            {
                crapsResult = true;
                haveCrapsResult = true;
            }
            else if(currentGameStorage[currentRoll] == 7)
            {
                crapsResult = false;
                haveCrapsResult = true;
            }
            currentRoll += 1;
        }
        currentRoll -= 1;
        if(detailPrint == true)
        {
            cout << "Game " << currentGame << ": ";
            for(printingNumber = 0; printingNumber <= currentRoll; printingNumber += 1)
            {
                cout << currentGameStorage[printingNumber] << " ";
            }
            if(crapsResult == true)
            {
                cout << "win";
            }
            else /*if(crapsResult == false) is redundant */
            {
                cout << "lose";
            }
            cout << endl;
        }
        return crapsResult;
        

        【讨论】:

          猜你喜欢
          • 2021-01-06
          • 2021-08-30
          • 1970-01-01
          • 2019-09-15
          • 1970-01-01
          • 2014-01-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多