Basic Random Rolling Dice Java

前端 未结 5 962
栀梦
栀梦 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:48

    How about this as your rollDice method:

    public static int rollDice(int number, int nSides) {
        int count = 0;
        for(int i = 0; i < number; i++) {
            count += (int)(Math.random() * nSides) + 1;
        }
        return count;
    }
    

提交回复
热议问题