请看看下面的代码再次与随机问题
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int main()
{
enum Movement{STAND=1,WALK,RUN,CRAWL};
srand(time(0));
Movement state = (Movement)(1+rand()%4);
for(int i=0;i<10;i++)
{
cout << state << endl;
switch(state)
{
/*Here the logic is,
*
* 1. From stand, he can walk or crawl
2. From Walk, he can stand or run
3. From Run, he can walk
4. From Crawl, he can stand
*/
case STAND:
cout << "You can walk or crawl" << endl;
while(state==WALK || state==CRAWL)
{
state = (Movement)(1+rand()%4);
}
break;
case WALK:
cout << "You can stand or run" << endl;
while(state==STAND || state==RUN)
{
state = (Movement)(1+rand()%4);
}
break;
case RUN:
cout << "You can walk" << endl;
while(state==WALK)
{
state = (Movement)(1+rand()%4);
}
break;
default:
cout << "You can stand" << endl;
while(state==STAND)
{
state = (Movement)(1+rand()%4);
}
}
}
}
我使用随机的,并基于这些给定条件期望随机结果。但是我得到的结果如下。
2
You can stand or run
2
You can stand or run
2
You can stand or run
2
You can stand or run
2
You can stand or run
2
You can stand or run
2
You can stand or run
2
You can stand or run
2
You can stand or run
2
You can stand or run
这是为什么?我已经尝试过do..while循环。一点都不好。没有什么是检查我在病例陈述中给出的条件。请帮忙。
请学习如何使用调试器。 –