请看下面的代码
#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 循环。一点都不好。没有什么可以检查我在案例陈述中给出的条件。请帮忙。