Results 1 to 11 of 11
  1. #1
    newbit is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default 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;
    }
    }






    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please wrap your code in code tags so we can read it more easily.

    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. #3
    newbit is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Sorry bout that, yea that's where i'm getting an error.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  5. #5
    newbit is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    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. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  7. #7
    newbit is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Again, I'm very new to programming. So honestly I don't know how to answer your question. :-/

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

  10. #10
    newbit is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    I appreciate your guys' help, thanks!

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You're welcome, please mark your thread solved with the thread tools at the top if you are satisfied.

Similar Threads

  1. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  2. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM
  3. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM

Posting Permissions

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