Results 1 to 11 of 11
Thread: Need help with Do while Y/N loop
- 04-12-2011, 06:18 AM #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;
}
}
}
- 04-12-2011, 06:27 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Please wrap your code in code tags so we can read it more easily.
Is where I am assuming you are having a problem, correct?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); } }
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 #3
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
Sorry bout that, yea that's where i'm getting an error.
- 04-12-2011, 06:44 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #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!
- 04-12-2011, 06:51 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
When will this be true?Java Code:while (false);
- 04-12-2011, 07:07 AM #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. :-/
- 04-12-2011, 07:11 AM #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.
- 04-12-2011, 07:14 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
With that explanation by junky I'm sure you can figure it out.
- 04-12-2011, 07:29 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 5
- Rep Power
- 0
I appreciate your guys' help, thanks!
- 04-12-2011, 07:40 AM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
[Q] Loop issue (while loop)
By iriscience in forum New To JavaReplies: 9Last Post: 01-31-2011, 04:21 PM -
Convert do while loop to for loop
By sandeeptheviper in forum New To JavaReplies: 3Last Post: 01-03-2011, 12:37 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
while-loop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06-26-2009, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks