Question: 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

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!