【问题标题】:Why are my if statements not working consistently?为什么我的 if 语句不能始终如一地工作?
【发布时间】:2017-05-30 17:50:31
【问题描述】:

我正在为我的 c++ 类制作一个抛硬币程序,我们需要制作一个函数来抛硬币并打印出正面或反面,每行打印 10 个。当我运行程序时,虽然我用来检测硬币是正面还是反面的 if 语句不足以从两者中挑选出来。

#include <iostream>
#include <ctime>
using namespace std;

void coinToss(int times);

int main()
{
srand(time(0));
    int times;
    cout << "How many times would you like to toss the coin?" << endl;
    cin >> times;

coinToss(times);

return 0;
}

void coinToss(int times)
{
    int toss = 0, count = 0;
    for(int i = 0; i < times;i++)
    {
        toss = rand()%2;

        if(toss == 1)//Detects if coin is heads.
        {
            cout << "H";
        }
        if(toss == 0)//Detects if coin is tails.
        {
        cout << "T";
        }

        else //I had to include this for the program to run, further explanation below the code.
        {
        cout << "Ya done goofed."; 
        }

        count++; //Counts to ten
        if(count == 10) //Skips to the next line if the coin has been tossed ten times.
        {
            cout << endl;
            count = 0;
        }
    }

}

有一次我用“cout

为了完成作业,我将第二个 if 语句更改为 else 语句,一切看起来都很好,但我真的很想了解这里发生了什么。

【问题讨论】:

  • if(toss == 0) ==> else if(toss == 0)
  • else 只适用于if (toss == 0),所以当toss == 1...
  • 你需要使用嵌套的 if..

标签: c++ if-statement coin-flipping


【解决方案1】:

你的代码会发生什么:

结果是 1 吗?然后打印 H。继续。结果是 0 吗?然后打印 T。否则,如果不为 0,则打印“Ya done goofed.”。

您需要将if 语句链接在一起:

if (toss == 1) {
    cout << "H";
} else if (toss == 0) {
    cout << "T";
} else {
    cout << "Ya done goofed.";
}

您不会再陷入else 案例中,并且可以将其移除。

作为旁注,关于您的整体程序结构:您的 coinToss 函数不应该做所有事情。您的代码应该更加拆分:返回 H 或 T 的函数,根据用户请求调用此函数 X 次并格式化输出的函数将是一个好的开始。

另一个小提示:您的 count 变量允许您每 10 次翻转添加一个新行,可以删除。 i % 10 会给你同样的结果:每增加十个增量,i % 10 将等于 0。

【讨论】:

    【解决方案2】:

    您可能正在正确打印输出,然后在最后一行没有写换行符的情况下终止,并且您的 shell 提示清除到左边距并覆盖您的输出(清除该行的其余部分以启动)。如果您的投掷次数少于 10 次,您的唯一输出行可能会丢失,否则将是最后一行。

    尝试在main returns 之前添加一个额外的std::cout &lt;&lt; '\n';

    (另外,你可以说 std::cout &lt;&lt; "HT"[rand() % 2];std::cout &lt;&lt; (rand() % 2 ? 'H' : 'T'); 并取消 ifs,但这没什么大不了的......在这个阶段你最清楚什么)

    【讨论】:

      【解决方案3】:

      好吧,rand()%2 只会产生两个数字:1 和 0,这似乎符合您的任务,因为硬币是布尔数字生成器,不是吗? :) 因此,这似乎可以完成您正在寻找的工作:

      #include <iostream>
      #include <ctime>
      using namespace std;
      
      void coinToss(int times);
      
      int main()
      {
      srand(time(0));
          int times;
          cout << "How many times would you like to toss the coin?" << endl;
          cin >> times;
      
      coinToss(times);
      
      return 0;
      }
      
      void coinToss(int times)
      {
          int toss = 0, Count = 0;
      
          for(int i = 0; i < times;i++)
          {
              toss = rand() % 2;
      
              // Choose:
              cout << ((toss) ? "H" : "T"); // if you want a character
              // or
              cout << toss;                 // if you want the number
      
              Count++; //Counts to ten
              if(Count == 10) //Skips to the next line if the coin has been tossed ten times.
              {
                  cout << endl;
                  Count = 0;
              }
          }
      }
      

      【讨论】:

      • Count 可以用i % 10 计算。
      • 是的,你保存了一个变量,但我只是不想过多地更改他的代码:)。
      【解决方案4】:
          if(toss == 1)//Detects if coin is heads.
          {
              cout << "H";
          }
          else if(toss == 0)//Detects if coin is tails.
          {
          cout << "T";
          }
      

      您需要使用 else-if 语句。你也不需要在toss==0 之后使用 else,因为 rand()%2 要么是 0 要么是 1。没有第三个选项。

      【讨论】:

        【解决方案5】:

        rand() 返回一个介于 0 和 RAND_MAX 之间的伪随机整数。而且,rand() % 2 将是 0 或 1。所以,会有:

        if(toss == 1)//Detects if head
        {
          cout << "H";
        }
        else // tail
        {
           cout << "T";
        }
        

        【讨论】:

          【解决方案6】:

          我不认为这有什么问题。好吧,我看不到...如果我添加一些调试,那么我会看到我认为您所期望的...

          #include <iostream> 
          #include <ctime> 
          using namespace std;
          
          void coinToss(int times);
          
          int main() { 
            srand(time(0)); 
            int times; 
          
            cout << "How many times would you like to toss the coin?" << endl; 
            cin >> times;
          
            coinToss(times);
          
            return 0; 
          }
          
          void coinToss(int times) { 
            int toss = 0, count = 0; 
          
            for(int i = 0; i < times;i++) { 
                toss = rand() % 2;
          
                cout << "Toss: " << toss << endl;
          
                if(toss == 1)//Detects if coin is heads. 
                { 
                    cout << "H (" <<  toss << ")" << endl; 
                } 
                if(toss == 0)//Detects if coin is tails. 
                { 
                    cout << "T (" <<  toss << ")" << endl; 
                }
          
                count++; //Counts to ten 
                if(count == 10) //Skips to the next line if the coin has been tossed ten times. 
                { 
                    //cout << endl; count = 0; 
                } 
            }
          }
          

          然后编译

          g++ coin_toss.cc
          

          然后运行它

          ./a.out 
          How many times would you like to toss the coin?
          4
          Toss: 1
          H (1)
          Toss: 0
          T (0)
          Toss: 0
          T (0)
          Toss: 0
          T (0)
          

          那么这正是我所期望的还是我错过了什么?

          您不需要“if else if”语句。

          【讨论】:

          • 你不需要它。但它更易于阅读和理解。
          【解决方案7】:

          您也可以使用开关:

           switch( rand() % 2 )
           {
            case 0:
               cout << "T";
               break;
          
            case 1:
               cout << "H";
               break;
          
            default:
                cout << "oops you goofed!;
          }
          // continue within for loop
          

          如果您在案例 1 之后“忘记”休息,您将再次收到“哎呀,你搞砸了!”每次抛头后的消息。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-01-14
            • 1970-01-01
            • 2015-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多