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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started