【问题标题】:Do-While Loop with random number generator带有随机数生成器的 Do-While 循环
【发布时间】:2023-02-06 13:59:57
【问题描述】:

躲猫猫是小孩子喜欢玩的有趣游戏。要在计算机上模拟这个游戏,请编写一个程序,生成一个 1 到 4 之间的随机数。然后,将与该数字相关联的动物名称打印到屏幕上。使用的动物名称将是:

生成 1 时的猪

生成a 2时的牛

生成 3 时的鸡

生成 4 时的马

如果您的程序生成 3,则输出将是:

如果玩家想再次玩游戏或输入其他任何内容以退出程序,则玩家将输入 1。

如果玩家输入“1 1 1 0”,输出将是:horse chicken cow horse

为了简化编码,在每个输出动物后跟一个空格,即使是最后一个。

提示:为了使测试更容易,将 0 作为随机数生成器的种子。另外,在重复游戏之前尝试执行一次游戏迭代。

为什么只输出三只动物却要输出四只动物?????。

#include <cstdlib>

#include<ctime>

using namespace std;
int main() {
  srand(0);
  int userChoice = 1;
  int option;
  cin >> userChoice;
  do {
    option = rand() % 4 + 1;
    if (option != 0) {
      if (option == 1) {
        cout << "pig" << " ";
      } else if (option == 2) {
        cout << "cow" << " ";
      } else if (option == 3) {
        cout << "chicken" << " ";
      } else if (option == 4) {
        cout << "horse" << " ";
      } else {
        break;
      }
    }
    cin >> userChoice;
  } while(userChoice != 0);
  return 0;
}


【问题讨论】:

  • 我在这里看不到问题。
  • 它只输出三只动物,但它需要输出四只。
  • @NathanPierson:问题“发布的代码中有什么错误?”暗示。
  • if (option != 0) 总是正确的。 break 永远联系不上。
  • 哦,问题不在于缺少特定的动物。那是你没有打印足够多的动物。检查你的循环逻辑。考虑用户立即输入 0 的情况。这应该打印一只动物。

标签: c++


【解决方案1】:

问题/改进:

  • 缺少&lt;iostream&gt;包括。
  • cin &gt;&gt; userChoice 之前 do 不是必需的。
  • break 从未联系过
  • 将动物和空间组合在一个字符串中,例如"horse "

代码:

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

int main() {
  srand(0);
  int userChoice;
  int option;
//   cin >> userChoice;
  do {
    option = rand() % 4 + 1;

    if (option == 1) {
        cout << "pig ";
    } else if (option == 2) {
        cout << "cow ";
    } else if (option == 3) {
        cout << "chicken ";
    } else if (option == 4) {
        cout << "horse ";
    }
    cin >> userChoice;
  } while(userChoice != 0);
  return 0;
}

【讨论】:

  • option==0 永远不会是真的。它始终是 1-4 之间的值。
  • @selbie,是的有道理。编辑了代码。
【解决方案2】:

在给定示例中有 3 个用户输入“1”的实例,导致生成 3 个动物名称(每个“1”输入一个)。 预期有 4 个输出的事实表明在执行文件时应该有一个初始输出:

running the executable -> output
inputting 1            -> output
inputting 1            -> output
inputting 1            -> output
inputting 0            -> quit

您可能需要以这样的方式编辑您的代码,使其最初生成输出,然后提示用户是否其他需要输出 - The player will then enter a 1 if they would like to play again 的措辞也暗示了这一点。

最初,不需要提示用户,因此您可以删除该行:

#include <cstdlib>

#include<ctime>

using namespace std;
int main() {
  srand(0);
  int userChoice = 1;
  int option;
    //cin >> userChoice; <-- remove this
  do {
    option = rand() % 4 + 1;
    if (option != 0) {
      if (option == 1) {
        cout << "pig" << " ";
      } else if (option == 2) {
        cout << "cow" << " ";
      } else if (option == 3) {
        cout << "chicken" << " ";
      } else if (option == 4) {
        cout << "horse" << " ";
      } else {
        break;
      }
    }
    cin >> userChoice;
  } while(userChoice != 0);
  return 0;
}

【讨论】:

    【解决方案3】:

    甚至可以进一步简化:

    #include <iostream>
    using namespace std;
    
    int main() {
      srand(0);
      int option;
      int userChoice;
      do {
        const char* animals[] = {"", "pig", "cow", "chicken", "horse"};
        option = rand() % 4 + 1;
        cout << animals[option] << " ";
        cin >> userChoice;
      } while(userChoice != 0);
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-15
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多