# Need help with Do while Y/N loop

• 04-12-2011, 06:18 AM
newbit
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;
}
}

}
• 04-12-2011, 06:27 AM
sunde887

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?
• 04-12-2011, 06:38 AM
newbit
Sorry bout that, yea that's where i'm getting an error.
• 04-12-2011, 06:44 AM
sunde887
Do you see what the error is? pay special attention to the condition, when will that line be true?
• 04-12-2011, 06:48 AM
newbit
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!
• 04-12-2011, 06:51 AM
sunde887
Code:

`while (false);`
When will this be true?
• 04-12-2011, 07:07 AM
newbit
Again, I'm very new to programming. So honestly I don't know how to answer your question. :-/
• 04-12-2011, 07:11 AM
Junky
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.
• 04-12-2011, 07:14 AM
sunde887
With that explanation by junky I'm sure you can figure it out.
• 04-12-2011, 07:29 AM
newbit
I appreciate your guys' help, thanks!
• 04-12-2011, 07:40 AM
sunde887