为什么我的 srand 返回所有结果
Why does my srand return all outcomes?
这是一个简单文本游戏的开始。在游戏中,你应该四处寻找地牢并收集文物。我用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";
}
}
你的if
语句并没有做你认为它们做的事情。
首先,当您应该改用==
比较运算符时,您使用的是=
赋值运算符。
其次,您使用的是 ,
运算符,该运算符计算左表达式和右表达式,然后返回右表达式的结果。
所以,这段代码:
if (diceRoll = 1, 2, 3, 4, 5, 6)
{
...
}
if (diceRoll = 7, 8)
{
...
}
if (diceRoll = 9, 10)
{
...
}
有效地做与此相同的操作,这不是您想要的:
diceRoll = 1;
if (6)
{
...
}
diceRoll = 7;
if (8)
{
...
}
diceRoll = 9;
if (10)
{
...
}
您需要改为执行以下操作:
if ((diceRoll == 1) ||
(diceRoll == 2) ||
(diceRoll == 3) ||
(diceRoll == 4) ||
(diceRoll == 5) ||
(diceRoll == 6))
{
cout << "You entered a dungeon! n";
}
else if ((diceRoll == 7) ||
(diceRoll == 8))
{
cout << "Oh No! An enemy has arrived! n";
}
else
{
cout << "You found an artifact! n";
}
这可以使用范围比较来简化:
if ((diceRoll >= 1) && (diceRoll <= 6))
{
cout << "You entered a dungeon! n";
}
else if ((diceRoll >= 7) && (diceRoll <= 8))
{
cout << "Oh No! An enemy has arrived! n";
}
else
{
cout << "You found an artifact! n";
}
或者替换为单个switch
语句:
switch (diceRoll)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
{
cout << "You entered a dungeon! n";
break;
}
case 7:
case 8:
{
cout << "Oh No! An enemy has arrived! n";
break;
}
case 9:
case 10:
{
cout << "You found an artifact! n";
break;
}
}
另外,附带说明一下,您不应该每次调用mainScreen()
时都调用srand()
(我认为在程序的生命周期中可以多次调用(。 srand()
只应调用一次,因此在调用mainScreen()
之前,您应该在main()
中调用它。
这个:
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";
}
是完全错误的,你需要单独检查每个元素,像这样:
if (diceRoll == 1 || diceRoll ==2 || diceRoll == 3 || diceRoll == 4 diceRoll == 5 || diceRoll == 6) {
cout << "You entered a dungeon! n";
}
if (diceRoll == 7|| diceRoll == 8) {
cout << "Oh No! An enemy has arrived! n";
}
if (diceRoll == 9 ||diceRoll == 10) {
cout << "You found an artifact! n";
}
为了进一步简化第一个分支,您可以这样做:
if (diceRoll >= 1 || diceRoll <= 6) {
cout << "You entered a dungeon! n";
}
相关文章:
- 如何返回一个类的两个对象相加的结果
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- C++Brute Force攻击函数不会返回结果
- 如何从递归函数中完全返回,该函数给出了每个函数结果的累积相加?
- 如何读取 C++ SAFEARRAY**,该 SAFEARRAY** 是 COM 互操作的结果,其中 C# 返回值为
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- c++ lambda:柯里和函数:使用按值捕获与按引用捕获返回不同的结果
- C++ 获取函数在常量引用中按值返回的结果
- 如何在一个表达式中生成并返回结果?
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- 三元运算符在返回语句中给出意外的结果
- 为什么函数 tellg() 没有返回好的结果?
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 有没有办法将 for 循环结果返回到像三元运算符这样的函数中?
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- 检查文本文件中的 0 或 1,并根据结果返回
- 如何使用boost::any_range连接多个boost范围并作为函数w/o的结果返回
- 如何从线程的结果返回Qt对象(Qtfutur)
- C++:当在函数内部创建新对象并将其作为结果返回时,我必须使用新运算符来创建对象