0

I have a question: how do I validate that the value entered in the correct range and ask the user to reenter the choice? I already have the code listed below but it keeps looping instead. For example, if player 1 enters 6, it should say incorrect, re-enter the correct number, and when player 1 enter a number between 1 and 5, it goes on to player 2 and repeats. My problem is the loop I used is not allowing me to do so, instead it is looping my print statement 5 times.

    import java.util.Scanner;
    import java.util.Random;
    public class Moropinzee
    {
       public class void main(String[] args)
       {
       String personPlay;
       int computerInt;
       String response; 


        int Monkey = 1;
        int Robot = 2;
        int Pirate = 3;
        int Ninja = 4;
        int Zombie = 5;
        Scanner scan = new Scanner(System.in);
        Random generator = new Random(); 

        System.out.print("Hey, let's play Moropinzee!\n" + 
                       "Please enter a move.\n");

        System.out.println("Player 1: Enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
        String player1 = scan.next().toUpperCase();


        System.out.println("Player 2: Enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie");
        String player2 = scan.next().toUpperCase();

        System.out.println();



      for(int number = 0; number < 5; number++)
      {
      System.out.println("Invaild choice, Player 1. Enter a number 1-5:");
      }
    }
  }

enter image description here

enter image description here

I want to give my gratitude on the person who helped me clean up my code. I ran into one problem, if I enter number 1, the code will print invalid try again... Well, you can see on the pictures down below. If I enter 1, it should move onto asking Player 2 to key in a number.

player 1 = nextInt();
if (player 1 =>6)
invalid try again

3 Answers3

0

I feel like using a while loop here would be more beneficial. Also, you misspelled "Invalid" in your last print line. Try using a while loop like this one:

// used to print invalid command if input other than those stated is 
    used.
while (!playerOne.equalsIgnoreCase("1") && 
    !playerOne.equalsIgnoreCase("2")) {
    System.out.println(invalidCommand);
    playerOne = scan.nextLine();
}

Try using this for every input that will be accepted (I only did 1 and 2), and create a variable called invalidCommand that will tell the user to try a valid input!

0

Replace your code with this one:

import java.util.Scanner;
import java.util.Random;
class Moropinzee
{
   public static void main(String[] args)
   {
    String personPlay;
    int computerInt;
    String response; 


    int Monkey = 1;
    int Robot = 2;
    int Pirate = 3;
    int Ninja = 4;
    int Zombie = 5;
    int player1 = 0;
    int player2 = 0;
    Scanner scan = new Scanner(System.in);
    Random generator = new Random(); 

    System.out.print("Hey, let's play Moropinzee!\n" + "Please enter a move.\n");
    while(!(player1>0) || !(player1<6)) {
        System.out.println("Player 1: Enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
        player1 = scan.nextInt();
    }


    while(!(player2>0) || !(player2<6)){
        System.out.println("Player 2: Enter a number 1-5 for Monkey, Robot, Pirate, Ninja, or Zombie:");
        player2 = scan.nextInt();
    }

//...........true..............
    System.out.println();
   }
}

Note that I've changed the variables player1 and player2 to int. So, change accordingly for further code.

Mohd Zaid
  • 659
  • 6
  • 14
  • How do I output winning conditions?! If player 1 and 2 input the same number, it should say draw. How do I code that!?!? – Ghetto Lisa Oct 05 '18 at 14:19
0

Try this out... if you don't understand something feel free to ask.

public static void main(String[] args) {
    int[] playerChoice = new int[5]; // 5 players
    try (Scanner scan = new Scanner(System.in)) {  //in try resource so it is closed when no longer needed.
        for (int i = 0; i < playerChoice.length; i++) { //loop over the players
            System.out.println("Player " + (i + 1));
            int intInput;
            try {
                intInput = scan.nextInt();//get the input in int
            } catch (InputMismatchException ime) { // input was not int
                intInput = 0;//assign bad number
            }
            if (intInput < 1 || intInput > 5) { //test
                System.out.println("Invalid input.");
                i--;//retry
            }
        }
    }
}
Charles
  • 944
  • 5
  • 9