Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So I am having alot of trouble getting these two programs to work Program 1 import java.util.Random; import java.util.Scanner; public class Main { public static

So I am having alot of trouble getting these two programs to work

Program 1

import java.util.Random;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

Random rand = new Random();

System.out.print("Enter player 1 name: ");

String player1name = scan.nextLine();

System.out.print("Enter player 1 max steps: ");

int player1maxsteps = scan.nextInt();

System.out.print("Enter player 2 name: ");

scan.nextLine();

String player2name = scan.nextLine();

System.out.print("Enter player 2 max steps: ");

int player2maxsteps = scan.nextInt();

PlayGame(player1name, player1maxsteps, player2name, player2maxsteps);

}

public static void PlayGame(String player1name, int player1maxsteps, String player2name, int player2maxsteps) {

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 if (player2total > player1total) {

System.out.println(player2name + " wins!");

} else {

System.out.println("It's a tie!");

}

}

}

Program 2

import java.util.Random;

public class Playgame {

public static void main(String[] args) {

Random rand = new Random();

System.out.print("Enter player 1 name: ");

String player1name = System.console().readLine();

System.out.print("Enter player 1 max steps: ");

int player1maxsteps = Integer.parseInt(System.console().readLine());

System.out.print("Enter player 2 name: ");

String player2name = System.console().readLine();

System.out.print("Enter player 2 max steps: ");

int player2maxsteps = Integer.parseInt(System.console().readLine());

System.out.print("Do you want to play a regular 30 step game or a different number of steps? (30/different) ");

String choice = System.console().readLine();

if (choice.equals("30")) {

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

} else {

System.out.print("Enter the number of steps to cross the finish line: ");

int finishLine = Integer.parseInt(System.console().readLine());

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

}

}

public static void PlayGame(String player1name, int player1maxsteps,

String player2name, int player2maxsteps, int finishLine) {

int player1total = 0;

int player2total = 0;

Random rand = new Random();

while (player1total < finishLine && player2total < finishLine) {

player1total += rand.nextInt(player1maxsteps) + 1;

player2total += rand.nextInt(player2maxsteps) + 1;

}

if (player1total > player2total) {

System.out.println(player1name + " wins!");

} else if (player2total > player1total) {

System.out.println(player2name + " wins!");

} else {

System.out.println("It's a tie!");

}

}

}

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 2 Lncs 13427

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124251, 978-3031124259

More Books

Students also viewed these Databases questions