public class Player {
static int count = 0;
String name;
Scanner sc;
Player(){
sc = new Scanner(System.in);
count = count + 1;;
System.out.print("Enter name: ");
name = sc.nextLine();
}
public static void main(String[] args) {
Player p1 = new Player();
System.out.println("Name: " + p1.name);
System.out.println("Count: " + p1.count);
Player p2 = new Player();
System.out.println("Name: " + p2.name);
System.out.println("Count: " + p2.count);
Player p3 = new Player();
System.out.println("Name: " + p3.name);
System.out.println("Count: " + p3.count);
}
}
Do C++ instead
Define a constructor for class Player.
When new Player() is invoked, your program should cause input/output in the following
format:
Enter player 1's name: Joewith joe being input from the player meaning that the player input his or her own name.
To achieve this, modify the static “count” field by adding 1 to it, to reflect that the total number of players has now increased by 1. Then print the prompt Enter player 1's name: Note
that you can refer to the “count” field here to print out the correct number of the current player.