Results 1 to 7 of 7
  1. #1
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Exclamation Help on Do-While loop

    So everything works in this number guessing game i wrote except for the do-while loop. When it asks you if you want to play again when you type n it works and displays "Thank You For Playing" but when you type in y it doesnt do anything. Can anyone tell me whats wrong and how to fix it?

    Sorry the codes a little long

    All help is appriecated very much:)

    Java Code:
    import java.io.*;
    import javax.swing.*;
    import java.util.Random;
    
    public class GameProject{
    
    
      
      public static void main(String[] args)throws IOException{
      
    
      	BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
      	Random rand= new Random();
        int random=rand.nextInt(50)+1;
        boolean playAgain=false;
        char response;
        int guess1,guess2,guess3,guess4,guess5,guess6;
        
        System.out.println("Guess a Number Between 1-50");
        System.out.println("You will have 6 guesses");
        System.out.println("I will tell you if you need to go higher or lower");
        System.out.println("Good Luck");
        System.out.println("Guess #1");
        guess1=Integer.parseInt(input.readLine());
        if(guess1==random)
        {
        	System.out.println("You won");
        	System.out.println("Would you like to play again?");
        	response=input.readLine().charAt(0);
        	if(response=='Y'||response=='y')
        		playAgain=true;
        	if (response=='N'||response=='n')
        		System.out.println("Thank You For Playing!");
        } 
        
         
        if(guess1<random)
        	System.out.println("Go higher");
        if(guess1>random)
        	System.out.println("Go lower");
        if(guess1<random||guess1>random)
       	System.out.println("Please Guess Again");
       	 if(guess1<random||guess1>random)
       	System.out.println("Guess #2");
        guess2=Integer.parseInt(input.readLine());
        if(guess2==random)
        	{
        	System.out.println("You won");
        	System.out.println("Would you like to play again?");
        	response=input.readLine().charAt(0);
        	if(response=='Y'||response=='y')
        		playAgain=true;
        	if (response=='N'||response=='n')
        		System.out.println("Thank You For Playing!");
            } 
        
    
        if(guess2<random)
        	System.out.println("Go higher");
        if(guess2>random)
        	System.out.println("Go lower");
       if(guess2<random||guess2>random)
       	System.out.println("Please Guess Again");
       	if(guess2<random||guess2>random)
       	System.out.println("Guess #3");
        guess3=Integer.parseInt(input.readLine());
        if(guess3==random)
        	{
        	System.out.println("You won");
        	System.out.println("Would you like to play again?");
        	response=input.readLine().charAt(0);
        	if(response=='Y'||response=='y')
        		playAgain=true;
        	if (response=='N'||response=='n')
        		System.out.println("Thank You For Playing!");
            } 
        	
       
        if(guess3<random)
        	System.out.println("Go higher");
        if(guess3>random)
        	System.out.println("Go lower");
       if(guess3<random||guess3>random)
       	System.out.println("Please Guess Again");
       	if(guess3<random||guess3>random)
       	System.out.println("Guess #4");
        guess4=Integer.parseInt(input.readLine());
        if(guess4==random)
        	{
        	System.out.println("You won");
        	System.out.println("Would you like to play again?");
        	response=input.readLine().charAt(0);
        	if(response=='Y'||response=='y')
        		playAgain=true;
        	if (response=='N'||response=='n')
        		System.out.println("Thank You For Playing!");
            }  
        	
        
        if(guess4<random)
        	System.out.println("Go higher");
        if(guess4>random)
        	System.out.println("Go lower");
        if(guess4<random||guess4>random)
       	System.out.println("Please Guess Again");
       	 if(guess4<random||guess4>random)
       	System.out.println("Guess 5");
        guess5=Integer.parseInt(input.readLine());
        if(guess5==random)
        	{
        	System.out.println("You won");
        	System.out.println("Would you like to play again?");
        	response=input.readLine().charAt(0);
        	if(response=='Y'||response=='y')
        		playAgain=true;
        	if (response=='N'||response=='n')
        		System.out.println("Thank You For Playing!");
            } 
        
        if(guess5<random)
        	System.out.println("Go higher");
        if(guess5>random)
        	System.out.println("Go lower");
        if(guess5<random||guess5>random)
       	System.out.println("Please Guess Again");
       	 if(guess5<random||guess5>random)
       	System.out.println("Guess 6");
        guess6=Integer.parseInt(input.readLine());
        if(guess6==random)
        	{
        	System.out.println("You won");
        	System.out.println("Would you like to play again?");
        	response=input.readLine().charAt(0);
        	if(response=='Y'||response=='y')
        		playAgain=true;
        	if (response=='N'||response=='n')
        		System.out.println("Thank You For Playing!");
            } 
        else
        {
       	System.out.println("You lose");
       	System.out.println("The number was: "+random);
       	if(playAgain==true)
       	 do{
       	
       	 	System.out.println("Good Luck");
        System.out.println("Guess #1");
        guess1=Integer.parseInt(input.readLine());
        if(guess1==random)
        	System.out.println("You won");
        if(guess1<random)
        	System.out.println("Go higher");
        if(guess1>random)
        	System.out.println("Go lower");
        if(guess1<random||guess1>random)
       	System.out.println("Please Guess Again");
       	 if(guess1<random||guess1>random)
       	System.out.println("Guess #2");
        guess2=Integer.parseInt(input.readLine());
        if(guess2==random)
        	System.out.println("You won");
    
        if(guess2<random)
        	System.out.println("Go higher");
        if(guess2>random)
        	System.out.println("Go lower");
       if(guess2<random||guess2>random)
       	System.out.println("Please Guess Again");
       	if(guess2<random||guess2>random)
       	System.out.println("Guess #3");
        guess3=Integer.parseInt(input.readLine());
        if(guess3==random)
        	System.out.println("You won");
       
        if(guess3<random)
        	System.out.println("Go higher");
        if(guess3>random)
        	System.out.println("Go lower");
       if(guess3<random||guess3>random)
       	System.out.println("Please Guess Again");
       	if(guess3<random||guess3>random)
       	System.out.println("Guess #4");
        guess4=Integer.parseInt(input.readLine());
        if(guess4==random)
        	System.out.println("You won");
        
        if(guess4<random)
        	System.out.println("Go higher");
        if(guess4>random)
        	System.out.println("Go lower");
        if(guess4<random||guess4>random)
       	System.out.println("Please Guess Again");
       	 if(guess4<random||guess4>random)
       	System.out.println("Guess 5");
        guess5=Integer.parseInt(input.readLine());
        if(guess5==random)
        	System.out.println("You won");
        if(guess5<random)
        	System.out.println("Go higher");
        if(guess5>random)
        	System.out.println("Go lower");
        if(guess5<random||guess5>random)
       	System.out.println("Please Guess Again");
       	 if(guess5<random||guess5>random)
       	System.out.println("Guess 6");
        guess6=Integer.parseInt(input.readLine());
        if(guess6==random)
        	System.out.println("You won");
        else
        {
       	System.out.println("You lose");
       	System.out.println("The number was: "+random);
        }
       	 }
        while(playAgain==true);
        
             
       	 }
        }
      }
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  2. #2
    curious is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Here's the correct version of the code. You were not use flag (playAgain) correctly and you were not using the concept of loop correctly. if you copying the same code two times then you are not using loop.

    Hope this helps.

    Java Code:
    import java.io.*;
    import javax.swing.*;
    import java.util.Random;
    
    public class GameProject{
    
    
      
      public static void main(String[] args)throws IOException{
      
    
        BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
        Random rand= new Random();
        int random=rand.nextInt(50)+1;
        boolean playAgain=true;
        char response;
        int guess1,guess2,guess3,guess4,guess5,guess6;
        while (playAgain)
        {
        System.out.println("Guess a Number Between 1-50");
        System.out.println("You will have 6 guesses");
        System.out.println("I will tell you if you need to go higher or lower");
        System.out.println("Good Luck");
        System.out.println("Guess #1");
        guess1=Integer.parseInt(input.readLine());
        if(guess1==random)
        {
            System.out.println("You won");
            System.out.println("Would you like to play again?");
            response=input.readLine().charAt(0);
            if(response=='Y'||response=='y')
                playAgain=true;
            if (response=='N'||response=='n'){
                playAgain=false;
                System.out.println("Thank You For Playing!");
            }
        } 
        
         
        if(guess1<random)
            System.out.println("Go higher");
        if(guess1>random)
            System.out.println("Go lower");
        if(guess1<random||guess1>random)
        System.out.println("Please Guess Again");
         if(guess1<random||guess1>random)
        System.out.println("Guess #2");
        guess2=Integer.parseInt(input.readLine());
        if(guess2==random)
            {
            System.out.println("You won");
            System.out.println("Would you like to play again?");
            response=input.readLine().charAt(0);
            if(response=='Y'||response=='y')
                playAgain=true;
            if (response=='N'||response=='n'){
                playAgain=false;
                System.out.println("Thank You For Playing!");
            }
            } 
        
    
        if(guess2<random)
            System.out.println("Go higher");
        if(guess2>random)
            System.out.println("Go lower");
       if(guess2<random||guess2>random)
        System.out.println("Please Guess Again");
        if(guess2<random||guess2>random)
        System.out.println("Guess #3");
        guess3=Integer.parseInt(input.readLine());
        if(guess3==random)
            {
            System.out.println("You won");
            System.out.println("Would you like to play again?");
            response=input.readLine().charAt(0);
            if(response=='Y'||response=='y')
                playAgain=true;
            if (response=='N'||response=='n'){
                playAgain=false;
                System.out.println("Thank You For Playing!");
            }
            } 
            
       
        if(guess3<random)
            System.out.println("Go higher");
        if(guess3>random)
            System.out.println("Go lower");
       if(guess3<random||guess3>random)
        System.out.println("Please Guess Again");
        if(guess3<random||guess3>random)
        System.out.println("Guess #4");
        guess4=Integer.parseInt(input.readLine());
        if(guess4==random)
            {
            System.out.println("You won");
            System.out.println("Would you like to play again?");
            response=input.readLine().charAt(0);
            if(response=='Y'||response=='y')
                playAgain=true;
            if (response=='N'||response=='n')
            {
                playAgain=false;
                System.out.println("Thank You For Playing!");
            }
            }  
            
        
        if(guess4<random)
            System.out.println("Go higher");
        if(guess4>random)
            System.out.println("Go lower");
        if(guess4<random||guess4>random)
        System.out.println("Please Guess Again");
         if(guess4<random||guess4>random)
        System.out.println("Guess 5");
        guess5=Integer.parseInt(input.readLine());
        if(guess5==random)
            {
            System.out.println("You won");
            System.out.println("Would you like to play again?");
            response=input.readLine().charAt(0);
            if(response=='Y'||response=='y')
                playAgain=true;
            if (response=='N'||response=='n'){
                playAgain=false;
                System.out.println("Thank You For Playing!");
            }
            } 
        
        if(guess5<random)
            System.out.println("Go higher");
        if(guess5>random)
            System.out.println("Go lower");
        if(guess5<random||guess5>random)
        System.out.println("Please Guess Again");
         if(guess5<random||guess5>random)
        System.out.println("Guess 6");
        guess6=Integer.parseInt(input.readLine());
        if(guess6==random)
            {
            System.out.println("You won");
            System.out.println("Would you like to play again?");
            response=input.readLine().charAt(0);
            if(response=='Y'||response=='y')
                playAgain=true;
            if (response=='N'||response=='n')
            {playAgain=false;
                System.out.println("Thank You For Playing!");
                
            }
            } 
        
        
        else
        {
        System.out.println("You lose");
        System.out.println("The number was: "+random);
        System.out.println("Would you like to play again?");
            response=input.readLine().charAt(0);
            if(response=='Y'||response=='y')
                playAgain=true;
            if (response=='N'||response=='n')
            {playAgain=false;
                System.out.println("Thank You For Playing!");
            }
           
        }
    }
    }
      }

  3. #3
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    Its still not replaying the game if you type in y and now n doesnt do anything
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may get more help if you format your code better. As displayed, it's difficult to see what is happening. Much luck!

  5. #5
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    What do you mean?
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by SwEeTAcTioN View Post
    What do you mean?
    You've written a program with many if / else blocks that use non-standard indentation. Since we more easily understand that which is familiar to us, if you posted the code with standard indentation, more folks would more easily understand your code at a glance, and be more willing to read it and help you that's all.

    You can find out more about Java standard coding conventions here: Coding Conventions Doc

  7. #7
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    Thanks i read it and it makes sense and i also fixed my code
    Are you suggesting that Cocunuts migrate?!! -Monty Python

Similar Threads

  1. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  2. Need help with a loop
    By ReV13 in forum New To Java
    Replies: 15
    Last Post: 12-01-2008, 05:48 PM
  3. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 03:29 PM
  4. How to use While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:44 PM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 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
  •