# Thread: Help on Do-While loop

1. ## 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{

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");
if(guess1==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess1<random||guess1>random)
System.out.println("Guess #2");
if(guess2==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess2<random||guess2>random)
System.out.println("Guess #3");
if(guess3==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess3<random||guess3>random)
System.out.println("Guess #4");
if(guess4==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess4<random||guess4>random)
System.out.println("Guess 5");
if(guess5==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess5<random||guess5>random)
System.out.println("Guess 6");
if(guess6==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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");
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)
if(guess1<random||guess1>random)
System.out.println("Guess #2");
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)
if(guess2<random||guess2>random)
System.out.println("Guess #3");
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)
if(guess3<random||guess3>random)
System.out.println("Guess #4");
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)
if(guess4<random||guess4>random)
System.out.println("Guess 5");
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)
if(guess5<random||guess5>random)
System.out.println("Guess 6");
if(guess6==random)
System.out.println("You won");
else
{
System.out.println("You lose");
System.out.println("The number was: "+random);
}
}
while(playAgain==true);

}
}
}```

2. Member
Join Date
Oct 2009
Posts
3
Rep Power
0
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{

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");
if(guess1==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess1<random||guess1>random)
System.out.println("Guess #2");
if(guess2==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess2<random||guess2>random)
System.out.println("Guess #3");
if(guess3==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess3<random||guess3>random)
System.out.println("Guess #4");
if(guess4==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess4<random||guess4>random)
System.out.println("Guess 5");
if(guess5==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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)
if(guess5<random||guess5>random)
System.out.println("Guess 6");
if(guess6==random)
{
System.out.println("You won");
System.out.println("Would you like to play again?");
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?");
if(response=='Y'||response=='y')
playAgain=true;
if (response=='N'||response=='n')
{playAgain=false;
System.out.println("Thank You For Playing!");
}

}
}
}
}```

3. Its still not replaying the game if you type in y and now n doesnt do anything

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

5. What do you mean?

6. 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

7. Thanks i read it and it makes sense and i also fixed my code

#### Posting Permissions

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