为什么我的 rand() 数字显示为 0
Why does my rand() number come up as 0?
这是我正在制作的类的函数成员,它不会导致任何语法错误,只是语义错误。它应该组成的随机数显示为 0。
int Warrior::attack()
{
int hit = 0;
switch (weapon)
{
case 6:
hit = rand() % (5 - 1 + 1) + 1;
break;
case 7:
hit = rand() % (10 - 4 + 1) + 4;
break;
case 8:
hit = rand() % (15 - 9 + 1) + 9;
break;
case 9:
hit = rand() % (20 - 14 + 1) + 14;
break;
case 10:
hit = rand() % (25 - 19 + 1) + 19;
break;
}
std::cout<< "You hit " << hit <<"!n";
return hit;
}
请帮忙!谢谢~武士刀
可能是缺少default
开关语句,即 weapon
不会触发任何switch
条款。
尝试:
switch (weapon)
{
case 6:
hit = rand() % (5 - 1 + 1) + 1;
break;
case 7:
hit = rand() % (10 - 4 + 1) + 4;
break;
case 8:
hit = rand() % (15 - 9 + 1) + 9;
break;
case 9:
hit = rand() % (20 - 14 + 1) + 14;
break;
case 10:
hit = rand() % (25 - 19 + 1) + 19;
break;
default:
throw std::exception("switch statement failed");
}
相关文章:
- 为什么在为其分配编号后无法显示我的字符串值?
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么
- 我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价
- 如何让我的代码显示文件名不正确或文件中数字的平均值?
- 如何重载运算符<<显示我的列表
- 为什么我无法显示我的链表?
- 为什么我的代码不显示我的 TButton 数组?
- 我如何显示我的字符串
- 如何在printf中显示我的源代码的行号
- 如何在Qt客户端应用程序中显示我的OSM磁贴
- 类,显示我的最终结果C++时出现问题
- 需要有关使用 for 循环显示我的数组的帮助
- 有没有一种方法可以在两列中显示我的素数列表输出
- 如何显示我的 Queue & Stack?
- 如何使用 OPENCV 显示我的代码中的图像
- 无法显示我的每个函数的结果
- 我如何让Anjuta突出显示我的构建系统(waf)产生的错误/行
- 如何在 GUI 输出窗口而不是 Visual Studio 2015 中的 cmd 控制台上显示我的C++程序输出
- 为什么 OpenGL 不显示我的火粒子