variable access outside of if statement

前端 未结 4 1831
南方客
南方客 2020-12-02 00:39

I am trying to access variable outside an if statement in java. The variable is axeMinDmg. Here is what i have but getting an error. I want minDmg = axeMi

4条回答
  •  不知归路
    2020-12-02 01:27

    If you want to assign a variable to outside of if-else block, you can use ternary operator which represented by the : operator.

    For example, the standard if-else Java expression:

    int money;
    if (shouldReceiveBonus()) {
        price = 100;
    }
    else {
        price = 50;
    }
    

    With ternary operator is equivalent to:

    int money = shouldReceiveBonus() ? 100 : 50;
    

提交回复
热议问题