随机变量总是生成5
What is supposed to be a random variable is always generating 5
所以我一直在自学如何编写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范围内的数字
相关文章:
- 将正态随机变量与任意 RHO(corrcoef) 相关联
- 为什么变量的打印地址在每次执行时都会打印随机值,即使它是 C 中的逻辑地址?
- 如何让变量随机输出四个单词之一
- 如何生成随机变量或类名并在以后使用?
- 尝试生成随机变量时获得"-nan(ind)"
- 为什么在 sync_with_stdio(false) 之前使用 cin 为下一个 i/p 变量提供随机值
- 如何使计算机每次在猪游戏中进行随机变量
- 在 c++ 中将随机生成的整数存储为变量
- 如何通过Mersenne Twister生成不重复的随机变量
- C 中的Bernoulli随机变量
- 快速在C 中添加随机变量
- MFC对话框成员变量的随机崩溃不在范围中
- 如何为Template变量给定随机值
- C++中的变量之间随机
- 为什么这个随机变量没有均匀分布?
- 函数返回看似随机的数字,而不是返回变量的值
- 随机更改变量
- 如何在 c++ 中生成介于 0 和变量之间的随机双精度
- C++ 随机将枚举类型分配给变量
- 返回一个带有随机选项和局部变量的 char 函数