Results 1 to 6 of 6
  1. #1
    javaboy2 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default How to end loop for game?

    I'm trying to end a loop for a ro-sham-bo game, but instead of asking for input, the program just breaks. I'm chobo :(
    Java Code:
    import java.util.Scanner;
    
    public class rps
    {
        public static void main (String[] args)
        {
            Scanner scan = new Scanner(System.in);    
            int rock = 1;
            int paper = 2;
            int scissors = 3;
            int input;
            String weapon1 = "a";
            String weapon2 = "a";
            String again = "y";
            while (again.equals("Y") || again.equals ("y"))
            {
                System.out.println ("Rock, paper, scissors! Pick your weapon: Rock = 1, Paper = 2, Scissors = 3");
                input = scan.nextInt();
                while (input < 1 || input > 3)
                {
                    System.out.println ("NOPE! Pick your weapon: Rock = 1, Paper = 2, Scissors = 3");
                    input = scan.nextInt();
                }
            
                if (input == 1)
                    weapon1 = "rock";
                if (input == 2)
                    weapon1 = "paper";
                if (input == 3)
                    weapon1 = "scissors";
            
                int opponent = (int)(Math.random() * 3 +1);
                if (opponent == 1)
                    weapon2 = "rock";
                if (opponent == 2)
                    weapon2 = "paper";
                if (opponent == 3)
                    weapon2 = "scissors";
            
            
            
                if  (input == opponent)
                {
                     System.out.println ("You both picked " + weapon1+ ". DRAW!");
                }
            
            
                else if (input == 1 && opponent == 2)
                {
                     System.out.println ("You picked " + weapon1+ ". The computer picked " +weapon2+". You should quit life.");
                }
                else if (input == 1 && opponent == 3)
                {
                    System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon2+". You win!");
                }
                
                else if (input == 2 && opponent == 1)
                {
                    System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon2+". You win!");
                }
                else if (input == 2 && opponent == 3)
                {
                    System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon2+". You win!");
                }
                else if (input == 3 && opponent == 1)
                {
                    System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon2+". You lose");
                }
                else if (input == 3 && opponent == 2)
                {
                    System.out.println ("You picked " +weapon1+ ". The computer picked " +weapon1+". You win");
                }
                System.out.println ("Play again? (Y/N)");
                again = scan.nextLine();
                
            } 
         }    
    }

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: How to end loop for game?

    Is it throwing an exception? If so, telling us exactly what the exception is and on what line it is thrown would be helpful.

  3. #3
    javaboy2 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: How to end loop for game?

    It doesn't throw an exception. It just ends the program. It won't ask the user to input anything after printing "Play again" and just ends.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: How to end loop for game?

    Looks like you have fallen for the pitfall caused by nextInt not consuming an end of line character when a user enters their choice. For a full explanation do a search. Simple solution make a call to nextLine after the call to nextInt.

  5. #5
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: How to end loop for game?

    Hmm, I'm not sure, but I suspect the nextInt() method leaves a superfluous newline in the input buffer. To fix, change both instances of scan.nextInt() to Integer.parseInt(scan.nextLine());

  6. #6
    javaboy2 is offline Member
    Join Date
    Oct 2011
    Posts
    3
    Rep Power
    0

    Default Re: How to end loop for game?

    Quote Originally Posted by Junky View Post
    Looks like you have fallen for the pitfall caused by nextInt not consuming an end of line character when a user enters their choice. For a full explanation do a search. Simple solution make a call to nextLine after the call to nextInt.
    This did the trick! Thanks guys

Similar Threads

  1. HiQ game ENDLESS LOOP HELP PLEASE !!
    By Bgreen7887 in forum New To Java
    Replies: 2
    Last Post: 10-08-2011, 02:27 AM
  2. What's wrong with this game loop?
    By Jacob_ in forum Java Gaming
    Replies: 1
    Last Post: 06-02-2011, 01:27 AM
  3. Guessing Game with for loop
    By ccart62 in forum New To Java
    Replies: 3
    Last Post: 11-22-2010, 01:55 AM
  4. Need Some Help, Simple Loop For Hangman Game
    By Juo in forum New To Java
    Replies: 2
    Last Post: 11-14-2009, 08:51 PM
  5. [help] game loop
    By Fuzzier in forum Java Applets
    Replies: 3
    Last Post: 10-31-2009, 09:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •