我目前正在编写C ++ Monty Hall问题模拟,并遇到了一些麻烦。我一直得到的错误是:
source.cpp(23): error C4700: uninitialized local variable 'doorReveal' used
source.cpp(25): error C4700: uninitialized local variable 'doorSwitch' used
source.cpp(52): error C4700: uninitialized local variable 'stayWin' used
source.cpp(56): error C4700: uninitialized local variable 'switchWin' used
我似乎无法弄清楚出了什么问题。该项目应该通过首先在前100次尝试中保持原始门选择然后在接下来的100次尝试中显示门时切换来模拟胜利。感谢大家的帮助。
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
int doorChoice;
int prizeDoor;
int doorReveal;
int doorSwitch;
int count;
int switchWin;
int stayWin;
srand((unsigned int) time(0));
for (count = 0; count <= 200; count++)
{
prizeDoor = (rand() % 3) + 1;
doorChoice = (rand() % 3) + 1;
cout << "The prize door is door number " << prizeDoor << " ." << endl;
cout << "The door the contestant chose was door " << doorChoice << endl;
doorReveal != prizeDoor || doorChoice;
cout << "The host revealed door number " << doorReveal << " ." << endl;
doorSwitch != doorChoice || doorReveal;
while (count < 101)
{
if (doorChoice == prizeDoor)
{
cout << "Great Job! You won!" << endl;
}
else {
cout << "Not this time!" << endl;
}
}
while (count < 201)
{
if (doorSwitch == prizeDoor)
{
cout << "You switched and won!" << endl;
}
else {
cout << "You switched and lost!" << endl;
}
}
if (doorChoice == prizeDoor)
{
stayWin++;
}
if (doorSwitch == prizeDoor)
{
switchWin++;
}
count++;
}
cout << "Your win percentage when staying was " << stayWin << "%!" << endl;
cout << "Your win percentage when switching was " << switchWin << "%!" << endl;
return 0;
}
答案 0 :(得分:10)
operator!=
operator !=
没有按照您的想法行事。
你的意思是
doorReveal = !(prizeDoor || doorChoice);
cout << "The host revealed door number " << doorReveal << " ." << endl;
doorSwitch = (doorChoice || doorReveal);
我在确定doorReveal时感觉到另一个逻辑问题。我稍后会考虑这个。 编辑:见问题5
while
while循环也存在问题:
while(count < 101)
// ...
while(count < 201)
它们是无限循环,因为循环期间计数不会增加。我认为你的意思是if
,而不是while
。
switchWin
和stayWin
这些变量仅递增。就像@KonradRudolph建议的那样,
const
rand()%3
有偏见你应该使用统一的发行版。
见
std::uniform_int_distribution
作为练习,因为它可能超出了您的课程范围。 请记住在任何现实生活代码中使用。enum doors { door1 = 1, door2 = 2, door3 = 4, any = door1|door2|door3 };
所以你可以说:
doors const doorReveal = doors(~(prizeDoor | doorChoice) & any);
doors const doorSwitch = doors(~(doorChoice | doorReveal) & any);
修复导致程序看起来有效:
#include <iostream>
#include <time.h>
using namespace std;
enum doors { door1 = 1, door2 = 2, door3 = 4, any = door1|door2|door3 };
static inline std::ostream& operator<<(std::ostream& os, doors val) {
switch(val) {
case door1: return os << "door #1";
case door2: return os << "door #2";
case door3: return os << "door #3";
case any: return os << "any door";
}
return os << "OOPS";
}
int main()
{
unsigned switchWin = 0;
unsigned stayWin = 0;
srand((unsigned int) time(0));
for(int count = 0; count <= 200; count++)
{
doors const prizeDoor = doors(1 << rand() / ( RAND_MAX / 3 ));
doors const doorChoice = doors(1 << rand() / ( RAND_MAX / 3 ));
cout << "The prize door is door number " << prizeDoor << " ." << endl;
cout << "The door the contestant chose was door " << doorChoice << endl;
doors const doorReveal = doors(~(prizeDoor | doorChoice) & any);
doors const doorSwitch = doors(~(doorChoice | doorReveal) & any);
cout << "The host revealed door number " << doorReveal << " ." << endl;
if(count < 101)
{
if(doorChoice == prizeDoor)
{
cout << "Great Job! You won!" << endl;
}
else
{
cout << "Not this time!" << endl;
}
};
if(count < 201)
{
if(doorSwitch == prizeDoor)
{
cout << "You switched and won!" << endl;
}
else
{
cout << "You switched and lost!" << endl;
}
};
if(doorChoice == prizeDoor)
{
stayWin++;
}
if(doorSwitch == prizeDoor)
{
switchWin++;
};
count++;
}
cout << "Your win percentage when staying was " << stayWin << "%!" << endl;
cout << "Your win percentage when switching was " << switchWin << "%!" << endl;
return 0;
}