По заданию нужно было сделать код как на картинке, но я, собрал вот такой, и хотел бы узнать, одинаковы ли они по функциональности и в чём плюсы или минусы моего варианта программы? Вот текст задания:
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:
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());
}
}
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты