Results 1 to 4 of 4
  1. #1
    DeathByStorm is offline Member
    Join Date
    Mar 2017
    Posts
    8
    Rep Power
    0

    Default Can't figure out how to add a playAgain loop onto my program.

    I'm trying to get this code to prompt "would you like you play again? (y/n)". It would then take the y letter and let the user play again. If user enters n it will end the game.

    Java Code:
    	public class G
    {
    	public static void main(String [] args)
    	{
    		Scanner S = new Scanner(System.in);																																//Introduction Statement to game
    		System.out.println("This is a guessing game.  I will ask you for two numbers.");
    		System.out.println("I will choose a random number between your two numbers for you " + "\n" + "to try and guess.  As you guess, I will give you hints.");
    
    		System.out.print("Choose two numbers to bound your range: ");																									//Scanner to store user's input for the range
    		int u1 = S.nextInt();
    		int u2 = S.nextInt();
    		int guess;
    		int count = 0; // = 0 new
    		String playAgain;
    
    		int high;
    		int low;
    		if(u1 < u2)
    		{
    			high = u2;
    			low = u1;
    		}
    		else
    		{
    			high = u1;
    			low = u2;
    		}
    		Random gen = new Random();
    		int rand = gen.nextInt(high-low+1) + low;
    		System.out.println("Now guess a number between " + low + " and " + high + ": ");
    		guess = S.nextInt();
    		while(guess != rand)
    		{
    			count++;
    			if (guess > high || guess < low)
    			{
    				System.out.println("Out of range. Please follow the directions dumb-ass.");
    			}
    			else if(guess > rand)
    			{
    				high = guess;
    				System.out.println("Too high!");
    			}
    			else
    			{
    				low = guess;
    				System.out.println("Too low!");
    			}
    
    			System.out.print("Now guess a number between " + low + " and " + high + ": ");
    			guess = S.nextInt();
    
    			if(guess == rand)
    			{
    				System.out.println("You got it!");
    			}
    			else if(count == 10)
    			{
    				System.out.println("You lost! So Sorry.");
    				break;
    			}
    		}
    
    		/*
    		do
    		{
    			System.out.println("Would you like to play again?");
    			playAgain = S.nextLine().toLowerCase().charAt(0);
    		}
    		*/
    	}
    }
    Last edited by DeathByStorm; 03-12-2017 at 06:45 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can't figure out how to add a playAgain loop onto my program.

    The code to be repeated needs to be inside of the loop.
    After doing executing the code to play, ask the user the question and exit the loop if the user says to exit.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DeathByStorm is offline Member
    Join Date
    Mar 2017
    Posts
    8
    Rep Power
    0

    Default Re: Can't figure out how to add a playAgain loop onto my program.

    Can you elaborate? I've been trying to put another loop in where
    Java Code:
    while (response.equalsIgnoreCase("y"))
    . I am going a bit crazy, I've been working on this ffor hours!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Can't figure out how to add a playAgain loop onto my program.

    The basic idea is:
    begin loop
    play game
    ask user if he wants to exit
    if user says to, exit loop
    end loop
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Cannot figure out basic nested for loop
    By no_slip_condition in forum New To Java
    Replies: 5
    Last Post: 06-06-2013, 05:18 AM
  2. Replies: 3
    Last Post: 06-28-2012, 08:58 PM
  3. Java Beginner: Can't Figure out Loop Array Issue.
    By CMDR Reservoir in forum New To Java
    Replies: 4
    Last Post: 07-19-2011, 02:30 AM
  4. Can't figure out how to write this program.
    By Quinn in forum New To Java
    Replies: 18
    Last Post: 05-18-2011, 09:04 PM
  5. Replies: 3
    Last Post: 03-09-2011, 02:55 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
  •