在if语句中未识别滚动骰子号
Rolling dice number not being recognized in if statement
我得到了一个小程序没有意识到,如果用户滚动6,他们会赢得折扣。当骰子最终掷出6时,它仍然将其视为失败,并告诉用户支付全部金额。我该如何解决这个问题?
我的班级:
class roll
{
private:
int high;
public:
roll(int high = 6)
{
this->high = high;
}
~roll()
{
}
int rolled(int amt = 1)
{
int done = 0;
for (size_t x = 0; x < amt; x++)
{
done += rand() % high + 1;
}
return done;
}
};
我的if语句:
cout << "Would you like to play a dice game for a discount? Y/N: " << endl;
cin >> res;
if (res == 'Y' || res == 'y')
{
srand(time(static_cast<unsigned>(0)));
roll one;
cout << one.rolled() << endl;
if (one.rolled() == 6)
{
cout << "Congratulations! You won 15% off your meal!!!" << endl;
prize = grandtot - (grandtot * .15);
cout << "Your final total will be $" << prize << endl;
}
else
{
cout << "Sorry, you did not win, pay the original amount!" << endl;
}
}
else
{
cout << "Thank you, pay the original amount and have a nice day!" << endl;
}
基本上,请查看@paulevans的答案。我想将重点放在您的rolled
功能上:
int rolled(int amt = 1)
{
int done = 0;
for (size_t x = 0; x < amt; x++)
{
done += rand() % high + 1; // <= This line
}
return done;
}
请注意您正在使用rand
函数获取随机值。的确,您可以通过使用此功能获得随机值,但我建议使用C 11方法 - 具有更好的分布(别忘了#include(:
int rolled(int amt = 1)
{
int done = 0;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(1,6); // distribution in range [1, 6]
for (size_t x = 0; x < amt; x++)
{
done += dist6(rng); // <= This line
}
return done;
}
有关更多详细信息,请参见:https://stackoverflow.com/a/13445752/8038186
您不是存储卷,而是想要这个:
const int current_roll = one.rolled();
cout << current_roll << endl;
if (current_roll == 6)
...
相关文章:
- 提升 ASIO 无法识别计时器对象
- 使用一个考虑到std::map中键值的滚动或换行的键
- 从udp接收帧对于人脸识别来说太慢
- 模板类无法识别友元运算符
- std::visit无法识别类型
- QScrollArea:由垂直滚动条引起的水平滚动条
- 跟踪滚动条上的鼠标事件
- 无法识别模板功能
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 如何在LLVM中dyn_cast以识别StoreInst?
- 从不同的附加依赖项中识别等同命名的函数
- C++出现控制台错误.我无法识别源代码的问题
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- 如何在不使用滚动条的情况下使视图更改
- Qt:"Q3DScatter"即使包含在内也无法识别
- 如何为对象生成滚动效果?
- 如何模拟不同边数的骰子滚动?
- 使用指针的类识别
- 如何静态识别动态堆分配?
- 在if语句中未识别滚动骰子号