Basic Random Rolling Dice Java

前端 未结 5 957
栀梦
栀梦 2021-01-05 11:31

I am trying to write a method rollDice(int number, int nSides) which returns the total result of rolling the number dice with nSides sides.

So for example rollDice(3

5条回答
  •  借酒劲吻你
    2021-01-05 11:51

    When you use % on a negative number you get a negative number.

    In this case the solution is simple, use

    int roll = r.nextInt(nSides) + 1; // 1 to nSizes
    num += roll;
    

提交回复
热议问题