Multiple conditions in if statement

巧了我就是萌 提交于 2021-01-29 04:11:24

问题


No matter what number is generated here I always get the first option (penguins). I can't seem to see any problem with my code, anyone else see what's wrong?

{
    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;
    }
}

回答1:


The code fragment prand == 1,5,9,10,14,15,19,20 is a sequence of expressions (the , is usually know as the comma operator), where the result of the first (or last — depending on language) expression only is used as a condition for the if statement. The remaining expressions are evaluated and their value is forgotten (please note this may lead to serious side effects in more complex situations.)

It's not very clear what language you are using, but in, say, C# you could use a switch statement to achieve what you want:

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;
}

Most languages have such a statement. See this wikipedia article for a general description.




回答2:


You're misusing the comma operator. The expression in the first if is:

if ( (prand == 1), (5), (9), (10), (14), (15), (19), (20) )

with each of the commas a comma operator. The definition of a comma operator is to evaluate the first expression (for possible side effects), then evaluate the second; the value of the expression is the value of the second expression. So your if becomes the exact equivalent of:

if ( 20 )

And 20 is implicitly converted to a bool, resulting in true.

Your compiler should have warned you about this. Something to the effect of a useless expression.




回答3:


if (prand == 1,5,9,10,14,15,19,20)

Although this is valid C++ and will compile, it does not do what you expect. You need to compare the variable to each value in turn:

if (prand == 1 || prand == 5 || prand == 9 || prand == 10 || prand == 14 || prand == 15 || prand == 19 || prand == 20)

This is because == is a binary operator which takes two values of compatible types.

In this situation, a switch...case statement is preferred as @Ondrej has explained.

I can think of at least two alternative ways to simulating a dice roll (which it appears you are trying to do:

  1. Use consecutive values for each option:

    if (prand >= 1 && prand <= 8) {
        // ...
    } else if (prand >= 9 && prand <= 13) {
        // ...
    } else if (prand >= 14 && prand <= 17) {
        // ...
    } else if (prand >= 18 && prand <= 20) {
        // ...
    } else {
        // Print an error message
    }
    
  2. Store the different possibilities in a std::list<std::set<int>>. Then you can iterate over the sets in the list and use the std::set.contains() method to check if the current set contains the value. This has the advantage of scalability. For example, it makes it easier to code the choices for 1d100 or other dice rolls with a large number of possible values.




回答4:


If it is "C" then You are testing the result of a comma operator. So the result of prand == 1,5,9,10,14,15,19,20, is the last element (BTW the first element is prand == 1). It is 20 which is always true.

I would suggest to build an array and check its elements...

enum Being_t {BEING_PENGUIN, BEING_TROLL, BEING_GOBLIN, BEING_WIZARD};
enum Being_t arr[20] = {BEING_PENGUIN, BEING_TROLL, BEING_GOBLIN, BEING_WIZARD,
    BEING_PENGUIN, BEING_TROLL, BEING_GOBLIN, BEING_WIZARD, ...};

Then You can use a switch

srand(time(0));
prand = (rand() % 20);
switch (arr[prand]) {
case BEING_PENGUIN:
    ...
    break;
...
}



回答5:


You should use or's or switch statements. For example, with if

if (prand == 1 || prand == 5 || prand == 9 || prand == 10 || prand == 14|| prand == 15|| prand == 19|| prand == 20)
{
        // your code here
}

and with switch

switch (prand)
{
    case 1:
    {

        // your code here

        break;
    }
    case 5:
    {

        // your code here

        break;
    }
    case 9:
    {

        // your code here

        break;
    }
    case 10:
    {

        // your code here

        break;
    }
    case 14:
    {

        // your code here

        break;
    }
    case 15:
    {

        // your code here

        break;
    }
    case 19:
    {

        // your code here

        break;
    }
    case 20:
    {

        // your code here

        break;
    }
}


来源:https://stackoverflow.com/questions/16281125/multiple-conditions-in-if-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!