无论在这里生成什么数字,我总是得到第一个选项(企鹅).我似乎无法看到我的代码有任何问题,其他人都看到了什么问题?
{
srand(time(0));
prand = (rand() % 20);
if (prand == 1,5,9,10,14,15,19,20){
entity = "penguins";
srand(time(0));
pquantity = (rand() % 8) + 2;
}
else if (prand == 2,6,11,16,18){
entity = "troll";
pquantity = 1;
}
else if (prand == 3,7,12,17){
entity = "goblin";
pquantity = 1;
}
else if (prand == 4,8,13){
entity = "wizard";
pquantity = 1;
}
}
Run Code Online (Sandbox Code Playgroud)
Ond*_*cny 10
代码片段prand == 1,5,9,10,14,15,19,20是一系列表达式(,通常称为逗号运算符),其中第一个(或最后一个 - 取决于语言)表达式的结果仅用作if语句的条件.评估剩余的表达式并忘记它们的值(请注意,这可能会在更复杂的情况下导致严重的副作用.)
目前还不是很清楚你使用的是什么语言,但是在C#中你可以使用switch语句来实现你想要的东西:
switch (prand)
{
// first set of options
case 1:
case 5:
…
case 20:
// your code here
break;
// second set of options
case 2:
case 6:
…
case 18:
// your code here
break;
default:
// all other options not listed above
break;
}
Run Code Online (Sandbox Code Playgroud)
大多数语言都有这样的陈述.有关一般说明,请参阅此维基百科文章.