请查看以下代码
#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循环。一点也不好。什么都没有检查我在案件陈述中给出的条件。请帮忙。
答案 0 :(得分:2)
将你的while循环翻转为do-while。表达式对于检查也是无效的(除非您的意图与文本不匹配)。根据消息,状态为:
Stand ==> (Walk || Crawl)
Walk ==> (Stand || Run)
Run ==> (Walk)
Crawl ==> (Stand)
因此需要将这些部分更改为
后一部分对于Run和Crawl状态很重要。因为它们只能产生一个有效的结果状态,所以旋转rand()调用寻找该值是没有意义的。只需设置新状态并再次循环。
关于上述(2):
case WALK:
cout << "You can stand or run" << endl;
while(state==STAND || state==RUN)
{
state = (Movement)(1+rand()%4);
}
break;
...变为
case WALK:
cout << "You can stand or run" << endl;
do {
state = (Movement)(1+rand()%4);
} while(state!=STAND && state!=RUN);
break;
关于运行和抓取状态:
case RUN:
cout << "You can walk" << endl;
state = WALK;
break;
default: // CRAWL
cout << "You can stand" << endl;
state = STAND;
break;
这让你再检查一下,我留给你了。
答案 1 :(得分:0)
解决方案:将Movement state = (Movement)(1+rand()%4);
移至for
循环。
更正后的代码:
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int main()
{
enum Movement{STAND=1,WALK,RUN,CRAWL};
srand(time(0));
for(int i=0;i<10;i++)
{
Movement state = (Movement)(1+rand()%4);
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);
}
}
}
return 0;
}
输出:
3
You can walk
1
You can walk or crawl
2
You can stand or run
1
You can walk or crawl
2
You can stand or run
3
You can walk
4
You can stand
2
You can stand or run
2
You can stand or run
4
You can stand
Press any key to continue
答案 2 :(得分:0)
您还可以在状态机中计算下一步:
...
case STAND:
cout << "You can walk or crawl" << endl;
state = rand()%2 ? WALK : CRAWL;
break;
...
答案 3 :(得分:-2)
rand()函数总是倾向于给出相同的“随机”值。
更好的方法是编写自己的RandomGenerator函数并使用它
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int RandomGenerator(int min, int max) // Generate min < random int < max
{
int randNum;
srand(rand()*time(NULL));
randNum = rand() % max + min;
// printf(" Random number is %d \n", randNum);
return randNum;
}
同时移动Movement state = (Movement)(1+rand()%4);
for
循环内的{{1}}