I am trying to create a very simple game in which several players of different types tries to guess a number in a range. I made a function to initialize an array of players, and I used switch-case to do it.
For some reason, if I put a number, it goes over all the cases after it, even if there are not enough cells in the array. For example, when entered 2, the value for type HUMAN, it did also the cases for 3 and 4- and players of types COMPUTER and GUMBLER are created.
Here is the code:
String name;
int count = 0;
System.out.println("How many players will participate?");
players = new Player[reader.nextByte()];
for (Player player: players)
{
count++;
System.out.print("\n1)Name of the player: ");
name = reader.next();
System.out.println("What will be its type? WRITE A NUMBER\n1- Monley\n2-Human\n3-Gumbler\n4-Computer");
switch (reader.nextInt())
{
case 1:
player = new Monkey (name, MAXIMUM, MINIMUM);
System.out.println("A moneky was created");
case 2:
player = new Human (name);
System.out.println("A human was created");
case 3:
player = new Gumbler (name, MAXIMUM, MINIMUM);
System.out.println("A gummbler was created");
case 4:
player = new Computer (name, MAXIMUM, MINIMUM);
System.out.println("A computer was created");
}
}
MAXIMUM
and MINIMUM
are the minimal and maximal numbers the number needs to be guessed can be. Human
, Monkey
, Computer
and Gumbler
are classes extending the absract class Player
. players
is the array of players (of type Player
).