我正在做一个非常简单的游戏。目的是将两个数字相加,将玩家的答案与计算机的答案进行比较,并告诉玩家答案是否正确。
int randnum = rand() % 100;
int number[2];
int answer = number[0] + number[1];
int playeranswer = 0;
number[0] = randnum;
number[1] = randnum * randnum - 23;
cout << number[0] << "+" << number[1] << endl;
cout << "What's your answer to the question?" << endl;
cin >> playeranswer;
if (playeranswer == answer)
{
cout << "Yep, that's the right answer" << endl;
}
else
{
cout << "Wrong" << endl;
cout << "The answer is..." << number[0] + number[1] << endl;
cout << "Your answer was..." << playeranswer << endl;
}
我希望它告诉我“是的,这是正确的答案”,但它告诉我“错”。 else
语句始终运行,即使两个数字明显相同。
我已经尝试用不同的变量替换数组。
我遇到的错误的一个很好的例子
答案 0 :(得分:1)
您正在将值分配给answer
变量,然后再将值分配给number[0]
和number[1]
。在将值分配给answer
和number[0]
之后,将值分配给number[1]
:
int randnum = rand() % 100;
int number[2];
int playeranswer = 0;
number[0] = randnum;
number[1] = randnum * randnum - 23;
int answer = number[0] + number[1];