Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Error I have 2 classes, Player.java class and Driver classed called Play.java. sample code Player.java: import java.util.Scanner; public class Play { public static void

Java Error

I have 2 classes, Player.java class and Driver classed called Play.java.

sample code Player.java:

import java.util.Scanner; public class Play {

public static void main(String[] args) { Scanner scan = new Scanner(System.in); int totalPlayers = Player.getUserPlayers(scan);

// array for each user Player[] players = new Player[totalPlayers];

// create each user for (int i = 0; i < players.length; i++) { players[i] = new Player(i + 1); }

Player.play(players);

}

}

sample code Play.java:

import java.util.Scanner;

public class Player { public static int userNumber; private static int playerNum;

Player(int pNumber){ playerNum = pNumber; } public int getPlayerNum() { return playerNum; } public static int getUserPlayers(Scanner c){ System.out.println(" " + "enter players from 2 to 4: "); userNumber = c.nextInt(); return userNumber; } public static void play(Player[] players) {

//use a for loop to welcome each player System.out.println("Welcome Player " +playerNum); }

}

------------------------------////---------------------------------

I am trying to create a play method that can return the player number (Player 1, Player 2, Player 3, Player 4) for each player in the game. So the user tells the system how many players there will be (a number from 2 -4) then an array for each player is created using the player class. Then an object is made for each user, where each user is given a name like Player 1, Player 2, Player 3, Player 4. so when I call the player[0] and ask for the player number (using getPlayerNum) it should return Player 1. Additionally, the play class should be able to display the player number of each player. Make sure it prints out a welcome for each player.

I am a beginner so please don't use code that is not in the basic level, and explain what you can using comments.

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

3319234609, 978-3319234601

More Books

Students also viewed these Databases questions

Question

How will the members be held accountable?

Answered: 1 week ago

Question

a. Do team members trust each other?

Answered: 1 week ago

Question

a. How will the leader be selected?

Answered: 1 week ago