【问题标题】:Simple Number Guessing Game. C++简单的猜数游戏。 C++
【发布时间】:2014-09-10 07:59:19
【问题描述】:

我一直在尝试制作一个简单的游戏,其中计算机生成一个随机数,然后您尝试猜测它。它还存储您“尝试”的猜测数量。

但是,当我运行该程序时,它只是打印:“让我们玩个游戏。我会想一个数字 1-100。试着猜一下。”

这是我的代码:

    #include <iostream>

    int main()

    {
        using namespace std;

        int the_number;
        int guess;
        int tries;

        the_number = rand() % 101 + 1;

        cout << "Let's play a game!";
        cout << "I will think of a number 1-100. Try to guess it.";
        cout << endl;
        cin >> guess;

        for (tries = 0; tries++;)
        {
            if (guess == the_number)
            {
                cout << "You guessed it!";
                cout << "And it only took you: " << tries;
            }
            else if (guess < the_number)
            {
                cout << "Higher";
                tries++;
            }


            else if (guess > the_number)
            {
                cout << "Lower";
                tries++;
            }

            else
                cout << "That's not even in range!";
            return 0;





    }



}

我不明白为什么这不起作用,有人可以解释为什么不起作用吗?

【问题讨论】:

  • for (tries = 0; tries++;)?

标签: c++ loops random


【解决方案1】:

您的程序在“让我们玩个游戏。我会想一个数字 1-100。试着猜猜”之后没有打印任何内容的原因。是您编写for 循环的方式。

for ( tries = 0; tries++; )

不做任何事情就跳出循环,因为tries++ 的计算结果为0

此外,为了让您的程序正常运行,您需要添加更多代码来读取猜测。类似下面的代码应该可以工作。

   for (tries = 0; ; tries++)
   {
      if (guess == the_number)
      {
         cout << "You guessed it!";
         cout << "And it only took you " << tries << " tries.\n";
         break;
      }
      else if (guess < the_number)
      {
         cout << "Higher";
         cin >> guess;
      }

      else if (guess > the_number)
      {
         cout << "Lower";
         cin >> guess;
      }
   }

【讨论】:

  • @R Sahu 实际上,它似乎总是每次都生成数字 42。有没有办法让它在我每次运行时生成一个新数字?
  • @Gold,您需要为该随机数生成器播种。您可以使用srand(time(NULL)) 或使用@w.b 的建议。
【解决方案2】:

您可以定义几个变量,使您的代码更易于理解,如下所示:

#include <iostream>
using namespace std;

int main()
{char EndGame = 'N';
    int MyNumber = 150 , playerguess;
    cout << "I have a number between 1 and 100.\nCan you guess my number ??\nPlease type your first guess.\n?" << endl;

    do{
        cin >> playerguess;
    if (playerguess > MyNumber) {
        cout << " Too High. Try again." << endl;

    }

    else if (playerguess == MyNumber) {
        cout << "Excellent ! You Got It ! \n If you want to exit press Y" << endl;
        cin >> EndGame;
        break;

    }
    else {
        cout << " Too Low. Try again." << endl;
    }
    } while (1);
return 0;
}

这将使数字等于 150。用户每次输入一个值,控制台都会判断它是否大于、小于或等于该数字。

如果您想每次都将其设为随机数,您可以简单地使用&lt;random&gt; 库并使用模块运算符来处理数字,例如 100 或 101。然后,您可以添加 1;这将只生成正整数。

【讨论】:

    【解决方案3】:

    你应该在这里使用while循环,而不是for

    while (the_number != guess)
    {
        //
        //
    }
    

    并尝试使用新的&lt;random&gt; 标头而不是rand() 函数:

    #include <random>
    
    std::random_device rd;
    
    std::default_random_engine engine(rd());
    std::uniform_int_distribution<int> uniform_dist(1, 100);
    
    the_number = uniform_dist(engine);
    

    【讨论】:

      【解决方案4】:

      你的 for 循环是错误的(它需要 3 件事:初始化、检查条件和每个循环后的待办事项步骤。 例如:

      for (tries = 0; tries < 5; tries++) 
      

      您还循环猜测部分,但您忘记向用户询问新数字。我建议将 cin &lt;&lt; guess 移到 for 循环中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多