# Thread: Need help with Do while Y/N loop

1. Member
Join Date
Apr 2011
Posts
5
Rep Power
0

## Need help with Do while Y/N loop

Hi guys, I'm very new to programming and I was hoping someone could help me on this problem. I need to ask the user if he/she wants to play again, If so, the program will repeat. If not, the program displays their original entry and winnings. I'm having problems with writing the Do-While y/n portion. And i'm just really lost. Any help is greatly appreciated. Thanks!

import java.util.Scanner;
import java.util.Random;

public class Chap6q12
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);

Random randomNumbers = new Random();

int num1, num2, num3, cash, winnings;

System.out.println("Please enter the amount you would like to gamble with: ");
cash = keyboard.nextInt();

num1 = randomNumbers.nextInt(6) + 1;
slot(num1);

num2 = randomNumbers.nextInt(6) + 1;
slot(num2);

num3 = randomNumbers.nextInt(6) + 1;
slot(num3);

if ((num1 == num2) || (num2 == num3) || (num1 == num3))
{
winnings = cash * 2;
System.out.println("Congratulations, you have doubled your money:" + winnings);
}
else if (num1 == num2 && num2 == num3)
{
winnings = cash * 3;
System.out.println("You hit the jackpot, you have tripled your money:" + winnings);
}
else
System.out.println("Bankrupt, LOSER!");

Boolean playAgain = false;
String again;

do
{
System.out.println("Would you like to place another bet (y=yes, n=no)?");
again = keyboard.nextLine();
if (again.equals("y"))
{
playAgain = true;
}
if (again.equals("n"))
{
playAgain = false;
}
}
while (false);
{
System.out.println("You started out with, " + cash + " and ended with, " + winnings);
}
}

public static void slot(int num)
{
switch(num)
{
case 1:
System.out.print("Cherries" + " ");
break;
case 2:
System.out.print("Dollars" + " ");
break;
case 3:
System.out.print("Plums" + " ");
break;
case 4:
System.out.print("Gold" + " ");
break;
case 5:
System.out.print("Bells" + " ");
break;
case 6:
System.out.print("Mellons" + " ");
break;
}
}

}

Java Code:
```Boolean playAgain = false;
String again;

do
{
System.out.println("Would you like to place another bet (y=yes, n=no)?");
again = keyboard.nextLine();
if (again.equals("y"))
{
playAgain = true;
}
if (again.equals("n"))
{
playAgain = false;
}
}
while (false);
{
System.out.println("You started out with, " + cash + " and ended with, " + winnings);
}
}```
Is where I am assuming you are having a problem, correct?

Your error is small, check the conditional of the loop, why are you using a definite value and not a variable?

3. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
Sorry bout that, yea that's where i'm getting an error.

4. Do you see what the error is? pay special attention to the condition, when will that line be true?

5. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
You're gonna have to tell me, because honestly I haven't been able to catch it these last few hours. I do appreciate the help!

6. Java Code:
`while (false);`
When will this be true?

7. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
Again, I'm very new to programming. So honestly I don't know how to answer your question. :-/

8. Your loop condition is false. Loops will only keep looping as long as the condition is true. Therefore your loop will only ever loop once then exit. You need to change the false to something else. Sure we can just spoonfeed you the answer but the point of the exercise is for you to think for yourself.

9. With that explanation by junky I'm sure you can figure it out.

10. Member
Join Date
Apr 2011
Posts
5
Rep Power
0
I appreciate your guys' help, thanks!

#### Posting Permissions

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