problem with a do while

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itsmee
    New Member
    • Oct 2007
    • 4

    problem with a do while

    I have a couple of issues with this program:
    1. i'm not sure on how to write so that if the users_num is greater than 21 it should ask if the user would want to play again. i tried putting break; after the if statement but that just exits out of the whole thing and goes to the main method and adding an else break would do the same thing.
    [code=java]
    betting_amount = in.nextInt();

    // computer generates the random number for the user by calling the randomNum function
    users_num = randomNum();
    if (users_num > 21)
    {
    System.out.prin tln("You busted. Your total is over 21. You lose!");
    balance = balance - betting_amount;
    System.out.prin tln("Your balance: $" +balance);
    }

    [/code]

    2. in this part i have the same problem. i'm not sure on what to add so that if users_num > 21 the user will get prompted if she/he will want to play again. then, the whole game will start again. I tried putting:
    [code=java]
    System.out.prin tln();
    System.out.prin tln("Would you like to play again? \nPress any key to continue.\nPres s M for MAIN MENU");
    play_again = myScanner.findW ithinHorizon(". ", 0).charAt(0);
    if (play_again == 'm' && play_again == 'M')
    {
    return balance;
    }

    // asks the user how much they want to bet
    System.out.prin tln("You have $" + balance + " balance.\nHow much would you like to bet?\n");
    [/code]
    into the if (users_num >21)but it doesn't work.

    [code=java]
    public static int twentyone(int balance)
    {
    // declared variables
    int betting_amount, users_num, num1, num2, r_num, choice;
    char play_again;

    Scanner myScanner = new Scanner(System. in);
    Scanner in = new Scanner(System. in);
    Random r = new Random();


    // asks the user how much they want to bet
    System.out.prin tln("You have $" + balance + " balance.\nHow much would you like to bet?");
    do
    {
    betting_amount = in.nextInt();

    // computer generates the random number for the user by calling the randomNum function
    users_num = randomNum();
    if (users_num > 21)
    {
    System.out.prin tln("You busted. Your total is over 21. You lose!");
    balance = balance - betting_amount;
    System.out.prin tln("Your balance: $" +balance);
    }

    // once the user's total is displayed and the total is greater than 21 then the user loses
    // otherwise the computer will ask if the user want to HIT or STAY.

    // user is asked if they want to HIT or STAY
    System.out.prin tln("1-Hit or 2-Stay");
    while (true)
    {


    // this will keep executing until the user presses either 1 to hit and 2 to stay
    do
    {
    choice = in.nextInt();
    if (choice != 1 && choice !=2)
    {
    System.out.prin tln("Please respond with (1) to HIT and (2) to STAY");
    choice = in.nextInt();
    }
    }while (choice != 1 && choice !=2);

    // checks user's choice: hit or stay
    if (choice == 2)
    {
    break; // the loop ends if the user decides to STAY
    }
    else if (choice == 1)
    {
    num1 = r.nextInt(14);
    users_num = users_num + num1;
    System.out.prin tln("Your card total is now: " +users_num);
    if (users_num > 21)
    {
    System.out.prin tln("You busted. Your total is over 21. You lose!");
    balance = balance - betting_amount;
    System.out.prin tln("Your balance: $" +balance);
    }
    else
    {
    System.out.prin tln("1-Hit or 2-Stay");
    }

    }
    }//end of while loop

    // Once the user decides to STAY or gets BUSTED
    // computer generates two random numbers, adds them until the total is greater than 16 and displays the total
    r_num = r.nextInt(23) +16;
    //num2 = r.nextInt(14);
    //r_num = num1 +num2;
    /*while (r_num <= 16)
    {
    num1 = r.nextInt(14);
    r_num += num1;
    } */
    System.out.prin tln("The dealer's total: " + r_num);

    // Now, we will compare the user's number with the computer's number and the winner is decided.
    System.out.prin tln();
    if (r_num > 21)
    {
    System.out.prin tln("Dealer busted. You win!");
    balance = balance + betting_amount;
    System.out.prin tln("Your balance: $" +balance);
    }
    else if (r_num == users_num)
    {
    System.out.prin tln("It's a draw. You lose.");
    balance = balance - betting_amount;
    System.out.prin tln("Your balance: $" +balance);
    }
    else if (r_num > users_num)
    {
    System.out.prin tln("You lose.");
    balance = balance - betting_amount;
    System.out.prin tln("Your balance: $" +balance);
    }
    else if (r_num < users_num)
    {
    System.out.prin tln("You win!");
    balance = balance + betting_amount;
    System.out.prin tln("Your balance: $" +balance);
    }

    // After winning or losing, the user will be asked if he/she will want to play again
    System.out.prin tln();
    System.out.prin tln("Would you like to play again? \nPress any key to continue.\nPres s M for MAIN MENU");
    play_again = myScanner.findW ithinHorizon(". ", 0).charAt(0);
    if (play_again == 'm' && play_again == 'M')
    {
    return balance;
    }

    // asks the user how much they want to bet
    System.out.prin tln("You have $" + balance + " balance.\nHow much would you like to bet?\n");

    } while (play_again != 'm' && play_again != 'M');

    return balance;

    } // end of TWENTY-ONE

    [/code]
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    do you think you could narrow down the problem?

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by MarkoKlacar
      Hi,

      do you think you could narrow down the problem?
      Perhaps you are doing the checking in the wrong place to begin with.

      If the twentyone method is the playTheGame method, then you could have the checking taking place where you call this method instead of in the method itself.

      Comment

      Working...