随机变量总是生成5

What is supposed to be a random variable is always generating 5

本文关键字:变量 随机      更新时间:2023-10-16

所以我一直在自学如何编写c++代码。我购买了Michael Dawson写的《Beginning c++ through game programming》第三版,其中有一章是关于循环的。在这一章的末尾,你制作了一款混淆单词的游戏。我想再深入一点,让它随机选择5个单词。但问题是,它总是得到5的值,因此总是选择第5个单词。

代码如下

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
    enum fields {WORD, HINT, NUM_FIELDS};
    const int NUM_WORDS = 5;
    const string WORDS[NUM_WORDS][NUM_FIELDS] =
    {
        {"wall", "Do you feel you're banging your head against something?"},
        {"glasses", "These might help you see the answer"},
        {"labored", "Going slowly is it?"},
        {"persistent", "Keep at it"},
        {"jumble", "It's what this game is about!"}
    };
    srand(time(0));
    int preWord = rand() % 3;
    string tempWord;
    string theWord = WORDS[preWord][WORD]; //word to guess
    string theHint = WORDS[preWord][HINT]; //hint for word
    if (preWord = 1)
    {
        theWord = "wall";
        tempWord = "wall";
    }
    if (preWord = 2)
    {
        theWord = "glasses";
        tempWord = "glasses";
    }
    if (preWord = 3)
    {
        theWord = "labored";
        tempWord = "labored";
    }
    if (preWord = 4)
    {
        theWord = "persistent";
        tempWord = "persistent";
    }
    if (preWord = 5)
    {
        theWord = "jumble";
        tempWord = "jumble";
    }
    int length = theWord.size();
    for (int i = 0; i < length; ++i)
    {
            int index1 = (rand() % length);
            int index2 = (rand() % length);
            char temp = theWord[index1];
            theWord[index1] = theWord[index2];
            theWord[index2] = temp;
    }
    cout << "tttWelcome to the Word Jumble!nn";
    cout << "Unscramble the letters to make a word.n";
    cout << "Enter 'hint' for a hint.n";
    cout << "Enter 'quit' to quit the game.nn";
    cout << "The word is: " << theWord;
    cout << "nvariable 'preWord' is: " << preWord;
    string guess;
    cout << "nnYour guess: ";
    cin >> guess;
    while ((guess != tempWord) && (guess != "quit"))
    {
        if (guess == "hint")
        {
            cout << theHint;
        }
        else
        {
            cout << "Sorry, that's not it.";
        }
        cout <<"nnYour guess: ";
        cin >> guess;
    }
    if (guess == tempWord)
    {
        cout << "nThat's it! You guessed it!n";
    }
    cout << "nThanks for playing!n";
    return 0;
}

在您的条件中,您应该这样写:

if (preWord == x)

比较,而不是

if (preWord = 5)

,这是一个赋值 -这意味着,当你的代码到达每一个if语句,它将实际分配值给你的变量-因为你的第五个情况,如果最后一个,它将最终改变preWord的值为5

您正在将preWord设置为5。if (preWord = 5)不检查其相等性。应该是if (preWord == 5)。使用警告进行编译,就不会出现这个问题了。

正如其他人指出的那样,您在if语句中使用赋值运算符(=)而不是相等运算符(==),因此您在每个if语句中设置preWord的值:

if (preWord = 5)
            ^
应:

if (preWord == 5)
            ^^

我养成的习惯是把文字放在等式的另一边,像这样:

if ( 5 == preWord )

如果你使用了=,这将是一个彻底的错误,在gcc中,你会看到这个消息:

error: lvalue required as left operand of assignment

当然,打开警告总是很重要的,即使你在你的代码中使用的形式,你会看到以下警告从gcc:

warning: suggest parentheses around assignment used as truth value

=为赋值,==为比较。所以你所有的if都需要使用==…所以改成if (preWord == 4)

also, int preWord = rand() % 3;将生成0到2范围内的随机数。您需要使用int preWord = rand() % 5 +1来生成1到5范围内的数字