Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions

Question

2. Discuss the evidence for psychopathy as a heritable disorder.

Answered: 1 week ago