Сравнить java-код по функциональности

235
09 марта 2019, 23:00

По заданию нужно было сделать код как на картинке, но я, собрал вот такой, и хотел бы узнать, одинаковы ли они по функциональности и в чём плюсы или минусы моего варианта программы? Вот текст задания:

Define a function called monopolyRoll(). This function provides a random result based on the dice-rolling rules for the board game Monopoly. In Monopoly, players roll two six-sided dice to determine their move. If they roll the same value on both dice, this is called “rolling doubles,” and it means they go again. In our simplified version, the dice-roll function should behave like this:

  1. Generate two random integers in the 1 to 6 range.
  2. If the numbers are not the same, return the sum.
  3. If the numbers are the same, generate two more random integers in the 1 to 6 range, and return the sum of all 4 numbers.

Hint: to make your code neater, you can define a second function that generates a random integer in the 1 to 6 range (or in the 1 to x range based on a parameter) so that you do not need to keep repeating that code.

public class Main {
    public static int monopolyRoll() {
       double randomNumber1 = Math.random();
       randomNumber1 = randomNumber1 * 6;
       randomNumber1 = randomNumber1 + 1;
       int randomInt1 = (int)randomNumber1;
       double randomNumber2 = Math.random();
       randomNumber2 = randomNumber2 * 6;
       randomNumber2 = randomNumber2 + 1;
       int randomInt2 = (int)randomNumber2;
       if (randomInt1 != randomInt2){
           int randomNumSum = randomInt1 + randomInt2;
           return randomNumSum;
       }
       else {
           double randomNumber3 = Math.random();
           randomNumber3 = randomNumber3 * 6;
           randomNumber3 = randomNumber3 + 1;
           int randomInt3 = (int)randomNumber3;
           double randomNumber4 = Math.random();
           randomNumber4 = randomNumber4 * 6;
           randomNumber4 = randomNumber4 + 1;
           int randomInt4 = (int)randomNumber4;
           int rollingDoublesSum = randomInt1 + randomInt2 + randomInt3 + randomInt4;
           System.out.println("It's a rolling doubles!");
           return rollingDoublesSum;
       }
    }
    public static void main(String[] args) {
        System.out.println(monopolyRoll());
    }
}
READ ALSO