# Help on Do-While loop

• 10-28-2009, 05:37 AM
SwEeTAcTioN
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:)

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);                         }     }   }```
• 10-28-2009, 06:07 AM
curious
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.

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!");         }           } } }   }```
• 10-29-2009, 04:19 AM
SwEeTAcTioN
Its still not replaying the game if you type in y and now n doesnt do anything
• 10-29-2009, 04:35 AM
Fubarable
You may get more help if you format your code better. As displayed, it's difficult to see what is happening. Much luck!
• 10-29-2009, 04:47 AM
SwEeTAcTioN
What do you mean?
• 10-29-2009, 04:56 AM
Fubarable
Quote:

Originally Posted by SwEeTAcTioN
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
• 10-30-2009, 05:09 AM
SwEeTAcTioN
Thanks i read it and it makes sense and i also fixed my code