我不断从代码中反复获得相同的结果
I keep repeatedly getting the same result from code
我想应该
像老虎机一样"旋转"水果并输出屏幕上的水果,出于某种原因,即使我的随机旋转整数确实发生了变化,它也只给了我苹果。我已经尝试逐步执行代码,但不确定我到底在寻找什么,出于某种原因,当我逐步执行时,它也给了我 w1.spin、w2.spin 和 w3.spin 作为相同的数字(对于 randomSpin)。
class Cspinner
{
private:
int randomSpin;
string FruitName;
int apple;
int orange;
int cherry;
int banana;
int peach;
public:
Cspinner()
{
srand(time(NULL));
apple = 30;
orange = 25;
cherry = 20;
banana = 15;
peach = 10;
}
Cspinner(int newapple, int neworange, int newcherry, int newbanana, int newpeach)
{
randomSpin = 0;
apple = newapple; //set apple to new value
orange = neworange; //set orange to new value
cherry = newcherry; //set cherry to new value
banana = newbanana; //set banana to new value
peach = newpeach; //set peach to new value
srand(time(NULL));
}
void spin()
{
randomSpin = rand() % 100 + 1;
if ((randomSpin >= 1) && (randomSpin <= apple))
{
FruitName = "apple ";
}
else if ((randomSpin > apple) && (randomSpin <= orange))
{
FruitName = "orange ";
}
else if ((randomSpin > orange) && (randomSpin <= cherry))
{
FruitName = "cherry ";
}
else if ((randomSpin > cherry) && (randomSpin <= banana))
{
FruitName = "banana ";
}
else if ((randomSpin > banana) && (randomSpin <= peach))
{
FruitName = "peach ";
}
}
void show()
{
cout << FruitName;
}
};
void main()
{
Cspinner w1;
Cspinner w2;
Cspinner w3(80, 5, 5, 5, 5);
for (int x = 0; x <= 9; x++)
{
w1.spin();
w2.spin();
w3.spin();
w1.show();
w2.show();
w3.show();
cout << endl;
}
system ("pause");
}
如果您打算将这些整数设置为百分比,请使它们累积。对于橙子的截止值,请使用您现在拥有的苹果和橙子的总和。对于樱桃,前三个(苹果、橙子和樱桃)的总和。如果希望用户能够输入百分比,请在用于设置阈值的函数(Cspinner构造函数)中表达此过程。
Apple 的值大于所有其他值。 然而,您首先要检查它。 只有两种可能的结果是苹果或什么都没有
相关文章:
- 这两个代码片段相似,但显示的结果不同
- 为什么第二个代码给出了预期的结果,而第一个代码却没有?
- RapidXML - 代码创建意外结果
- 代码未在联机编译器上显示结果
- 为什么这两段使用 constexpr、__PRETTY_FUNCTION__ 和 char * 的代码有不同的结果?
- C++位移位在相反方向上具有相同的常量,结果不同,代码更改很小
- 我的代码厨师提交显示错误的结果
- 如何查找导致结果不一致的代码
- 如何使用 c++ 将股票代码的结果添加到 poloniex 中的数组中?
- 我无法弄清楚我的代码未显示预期结果的问题
- 为什么这段代码(在 Matlab 的 MEX 文件中使用 OpenMP)给出不同的结果?
- 为什么这两个相似的代码计算行列式的结果不同?
- 为什么这个C++程序在代码::块和在线 IDE 之间返回不同的结果?
- C++:汇编代码包含断言结果
- 有人可以解释一下'sizeof'在此代码中返回的结果吗
- 相同的代码在联机 IDE 和本地 IDE 中给出不同的结果
- 来自 open() 的返回结果代码之间的差异:25 与 3
- 我不小心没有使用std::ref——结果代码做了什么?
- Typename给出了奇怪的输入结果代码块
- 用c++运行一个进程,过滤输出,获取结果代码,同时获取系统错误