【发布时间】:2014-03-17 03:21:11
【问题描述】:
所以基本上我正在尝试做的是将这段代码以某种方式变成一个 switch 语句。我不确定该怎么做。我有几个想法,比如用具有一系列值的关键字制作一个枚举(即“枚举野兔 { ONE =(范围从 1-5)TWO =(范围从 6-8)} - 但我不知道那是否是一个错误:S
希望我已经清楚我要问的问题了。
int y = 1 + rand() % 10;
// determine which move to make
if ( y == 1 || y == 2 ) {
hare += 0;
} else if ( y == 3 || y == 4 ) {
hare += 9;
} else if (y == 5) {
hare -= 12;
} else if (y >= 6 && y <= 8){
hare += 1;
} else if (y == 9 || y == 10){
hare -= 2;
}else {
++( hare );
}
if ( hare < 1 ) {
hare = 1;
} else if ( hare > RACE_END ) {
hare = RACE_END;
}
【问题讨论】:
-
要获得额外的学分,请学习新的 C++11 随机化引擎。
标签: c++ enums switch-statement