Why can't I use the ternary ? operator to select between two function calls?

后端 未结 4 1369
悲哀的现实
悲哀的现实 2020-12-03 23:24

I was recently programming and ran into an issue using the ? : operand. Here\'s my code.

    Random rand = new Random();
    for(int x = 0; x < 3; x++) {
         


        
4条回答
  •  青春惊慌失措
    2020-12-04 00:13

    a == 0 ? vertShip(board) : horizShip(board); // is an expression
    if (a == 0) vertShip(board); else horizShip(board); // is a statement
    

    Compare:

    if (condition) 
    {
        execute statement(s)
    }
    else
    {
        execute statement(s)
    }
    

    With:

     expression1 ? expression2 : expression3 
    

    Use the appropriate construct.

提交回复
热议问题