Question
Can you help me add comments to the following code: package testing; import java.util.Random; public class dice { public static void main(String[] args) { Random
Can you help me add comments to the following code:
package testing;
import java.util.Random;
public class dice {
public static void main(String[] args) { Random random = new Random(); int playerATotal = 0, playerBTotal = 0, playerCTotal = 0; boolean finished = false; while (!finished) { for (int currentPlayer = 1; currentPlayer <= 3;) { int roll1 = random.nextInt(6) + 1; int roll2 = random.nextInt(6) + 1;
System.out.println(String.format("Player %d rolls a %d and %d", currentPlayer, roll1, roll2)); if (currentPlayer == 1) { playerATotal += roll1 + roll2; System.out.println("Player " + currentPlayer + " has now " + playerATotal); if (playerATotal >= 15) { System.out.println("Player 1 wins the first place with a total of " + playerATotal); if (playerBTotal == playerCTotal) { System.out.println( "Both player 12 & player 3 wins the second place with a total of " + playerBTotal); } else if (playerBTotal > playerCTotal) { System.out.println("Player 2 wins the second place with a total of " + playerBTotal); System.out.println("Player 3 wins the third place with a total of " + playerCTotal); } else { System.out.println("Player 3 wins the second place with a total of " + playerCTotal); System.out.println("Player 2 wins the third place with a total of " + playerBTotal); } finished = true; break; } }
if (currentPlayer == 2) { playerBTotal += roll1 + roll2; System.out.println("Player " + currentPlayer + " has now " + playerBTotal); if (playerBTotal >= 15) { System.out.println("Player 2 wins the first place with a total of " + playerBTotal); if (playerATotal == playerCTotal) { System.out.println( "Both player 1 & player 3 wins the second place with a total of " + playerATotal); } else if (playerATotal > playerCTotal) { System.out.println("Player 1 wins the second place with a total of " + playerATotal); System.out.println("Player 3 wins the third place with a total of " + playerCTotal);
} else { System.out.println("Player 3 wins the second place with a total of " + playerCTotal); System.out.println("Player 1 wins the third place with a total of " + playerATotal); } finished = true; break; } } if (currentPlayer == 3) { playerCTotal += roll1 + roll2; System.out.println("Player " + currentPlayer + " has now " + playerCTotal); if (playerCTotal >= 15) { System.out.println("Player 3 wins the first place with a total of " + playerCTotal); if (playerBTotal == playerATotal) { System.out.println( "Both player 1 & player 2 wins the second place with a total of " + playerBTotal); } else if (playerATotal > playerBTotal) { System.out.println("Player 1 wins the second place with a total of " + playerATotal); System.out.println("Player 2 wins the third place with a total of " + playerBTotal); } else { System.out.println("Player 2 wins the second place with a total of " + playerBTotal); System.out.println("Player 1 wins the third place with a total of " + playerATotal); } finished = true; break; } } if (roll1 != roll2) { currentPlayer++; }
} } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started