【发布时间】:2017-01-08 17:06:27
【问题描述】:
这是一个简单的文字游戏的开始。在游戏中,你应该四处寻找地牢和收集文物。我用srand(time(0))来做一些事情,比如找什么阶段,攻击,你找到什么东西,我在编程方面还没有走多远,但我已经遇到了一个问题。我的 rand() 返回所有结果。当我运行游戏时(这不是完整的代码,顺便说一句),它会返回“你进入了地牢!”、“哦不,敌人来了!”和“你找到了神器!
void mainScreen()
{
srand(time(0));
cout << "Health: \n";
cout << health;
cout << endl;
_sleep(500);
cout << "Inventory: \n";
cout << inventory;
cout << endl;
_sleep(500);
cout << "Gold: \n";
cout << gold;
cout << endl;
_sleep(500);
cout << "Artifacts: \n";
cout << artifacts;
cout << endl;
_sleep(500);
cout << "Rolling the dice of fate... \n";
int diceRoll = 1 + (rand() % 10);
if (diceRoll = 1, 2, 3, 4, 5, 6) {
cout << "You entered a dungeon! \n";
}
if (diceRoll = 7, 8) {
cout << "Oh No! An enemy has arrived! \n";
}
if (diceRoll = 9, 10) {
cout << "You found an artifact! \n";
}
}
【问题讨论】:
-
返回所有结果是什么意思?另外,== 用于比较,||逻辑或
-
=和,不要做你认为他们做的事。尝试咨询有关 C++ 的参考资料,而不是猜测... -
diceRoll = 1, 2, 3, 4, 5, 6不是您检查多个值的方式 -
那我该怎么做呢?