当我测试轮盘赌机器人模拟器时,发生了一个奇怪的问题。 该计划非常简单,并使用带轮盘赌的投注站点的机会。它有一个自动系统,可以自动为您下注,以便找到我做这个程序的最佳设置,但遇到了前所未有的问题ID。当试验数低于约400时,在计算百分比时会出错。
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
cout << "how many trials would you like?" << endl;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
srand(time(0));
double yAmount = 0;
double bAmount = 0;
double rAmount = 0;
double gAmount = 0;
int finaltrial = 0;
int yellow = 1;
int blue = yellow + 10;
int red = blue + 17;
int grey = red + 26;
int money = 5000;
int trialno = 0;
int trials;
cin >> trials;
int result;
int bet = 10;
int moneyhigh = 0;
bool win;
bool bankrupt = false;
while (trialno < trials || trials == 0 && bankrupt == true)
{
result = rand() % 54 + 1;
if (result == yellow)
{
SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_GREEN);
cout << "Yellow";
yAmount++;
win = false;
}
else if (result <= blue)
{
SetConsoleTextAttribute(h, FOREGROUND_BLUE);
cout << "Blue";
win = false;
bAmount++;
}
else if (result <= red)
{
SetConsoleTextAttribute(h, FOREGROUND_RED);
cout << "Red";
win = false;
rAmount++;
}
else if (result <= grey)
{
SetConsoleTextAttribute(h, FOREGROUND_GREEN);
cout << "Green";
win = true;
gAmount++;
}
if (win == true)
{
money = money + bet;
bet = 10;
}
else
{
money = money - bet;
bet = bet * 2;
}
if (bet > money)
{
finaltrial = trialno;
trialno = trials - 1;
bankrupt = true;
}
if (money > moneyhigh)
{
moneyhigh = money;
}
cout << endl << " trial: " << trialno << " money: " << money << endl;
trialno++;
}
gAmount = gAmount / finaltrial ;
rAmount = rAmount / finaltrial ;
bAmount = bAmount / finaltrial ;
yAmount = yAmount / finaltrial ;
cout << endl << "Final: " << money << " Peak: " << moneyhigh << endl;
cout << "Green: " << gAmount << "% " << "Red: " << rAmount << "% " << "Blue: " << bAmount << "% " << "Yellow: " << yAmount << "% " << endl;
cin >> money;
return 0;
}
答案 0 :(得分:1)
在循环之前,用0初始化finaltrial
。仅在if (bet > money)
子句中编辑变量,这意味着如果语句永远不为真,则在循环结束时{{1 }将等于0,但是您将结果变量除以finaltrial,finaltrial
定义为double,gAmount
返回double / 0
答案 1 :(得分:0)
在C或C ++中,如果将double数据类型除以0,则返回inf。您的变量 finaltrial 仅在 if(bet&gt; money)返回 true时才会更新。当此条件失败时, finaltrial 保持为0, inf 会在这种情况下产生问题。
答案 2 :(得分:0)
您可能希望将finaltrial
初始化为1.
您已在程序开头将其初始化为0
,然后在满足特定条件时更新它。无论是否满足巡视条件,您都在分区操作中使用finaltrial
。当您进行除法操作时,您必须确保没有除0
,否则您将获得inf
。将finaltrial
初始化为1可帮助您消除获取inf
的问题(除非您将finaltrial
的值更新为0
)
答案 3 :(得分:0)
获得+ inf的唯一方法是使用正分子将浮点除以零。知道这将有助于你像兔子一样捕捉这个问题。 (注意,负分子除以零给出-Inf和0.0 / 0将评估为NaN - 而不是数字。)
显然,finalTrial为零,因为你只在最小的程序控制路径上改变它的值,所以这并不奇怪。