Question: **Using Java** Take the program below and modify it to handle another choice by the user. Copy the PlayGame() method and add a fifth parameter

**Using Java**

Take the program below and modify it to handle another choice by the user.

Copy the PlayGame() method and add a fifth parameter for the number of steps to cross the finish line. Change the loop to count up to this variable instead of the regular 30 the original version is still using.

[Making an overloaded method. You should have 2 versions of PlayGame() now]

Change main() right before it calls the PlayGame() method and ask the user of they want to play a regular 30 step game, or a different number of steps. Use an IF to call the first PlayGame() if they said they want to play a regular game, and for the variable distance choice ask them how many steps it will be and call the new version of PlayGame() and pass the number they give as the fifth variable.

public class Main { public static void PlayGame(String player1name, int player1maxsteps, String player2name, int player2maxsteps) { Random rand = new Random(); int player1total = 0; int player2total = 0;

while (player1total < 30 && player2total < 30) { player1total += rand.nextInt(player1maxsteps) + 1; player2total += rand.nextInt(player2maxsteps) + 1; }

if (player1total > player2total) { System.out.println(player1name + " wins!"); } else { System.out.println(player2name + " wins!"); } }

public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter player 1's name: "); String player1name = sc.nextLine(); System.out.print("Enter player 1's maximum number of steps per turn: "); int player1maxsteps = sc.nextInt(); sc.nextLine(); System.out.print("Enter player 2's name: "); String player2name = sc.nextLine(); System.out.print("Enter player 2's maximum number of steps per turn: "); int player2maxsteps = sc.nextInt();

PlayGame(player1name, player1maxsteps, player2name, player2maxsteps); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!