0

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).

The Roy
  • 2,178
  • 1
  • 17
  • 33
Asafwr
  • 197
  • 1
  • 3
  • 12
  • I suggest you read the Java tutorials on the Oracle web site. Here is an example https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html – Peter Lawrey Apr 17 '16 at 14:56

2 Answers2

5

End your each case block with a break.

Raman Shrivastava
  • 2,923
  • 15
  • 26
2

You will need a break statement in each case. See switch in java tutorial

    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");
                break; 
            case 2:
                player = new Human (name);
                System.out.println("A human was created");
                break;
            case 3:
                player = new Gumbler (name, MAXIMUM, MINIMUM);
                System.out.println("A gummbler was created");
                break;
            case 4:
                player = new Computer (name, MAXIMUM, MINIMUM);
                System.out.println("A computer was created");
        }
    }
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72