简体   繁体   中英

Looping an “If Else Statement” java

I am attempting to make a simple program in java using Scanner that will allow the user to shoot craps (play dice).

1.This code asks the user to enter how much money they have.

  1. The code will ask user to input a bet.

  2. Using a random number generator, it will inform the user what they rolled and inform the user how much money they've won/lost.

I have been able to successfully tell the computer to inform the user when they have won or lost, when they roll 2,3,7,11, or 12.

I am not sure how to tell the computer to allow the user to keep rolling the dice when other random values, such as 4,5,6,8,9, and 10 are rolled though, please help. Here's my code:

System.out.println("How much is in your purse?: ");
purse = input.nextInt();

System.out.println("make a bet: ");
bet = input.nextInt();

int pNumber = rand.nextInt(12) + 1;

if (pNumber == 2 || pNumber == 3 || pNumber == 12)
{
    purse = purse - bet;
    System.out.println("you rolled a " + pNumber);
    System.out.println("you lost $" + bet);
}
else if (pNumber == 7 || pNumber == 11)
{
    purse = purse + bet;
    System.out.println("you rolled a " + pNumber);
    System.out.println("you won $" + bet );
}
else
{
    System.out.println("you rolled a " + pNumber + ",keep rolling" );
}

Use an outer while loop to keep the game going until the user runs out of money.

Something like this. Start with an initial amount in the purse, then start the game.

If the user wins or loses, they make a new bet and roll again.

If the user neither wins nor loses, they just roll again with the current bet.

System.out.println("How much is in your purse?: ");


   purse = input.nextInt();
   boolean newBet = true;

    while(purse > 0)
    {
     if(newBet)
     {
      System.out.println("make a bet: ");
      bet = input.nextInt();
     }

     //roll the dice, new bet or not
     int pNumber = rand.nextInt(12) + 1;

     if ( pNumber == 2 || pNumber == 3 || pNumber == 12)
     {
            purse = purse - bet;
            System.out.println("you rolled a " + pNumber);
            System.out.println("you lost $" + bet);
            newBet =true; //make a new bet if you won
     }
     else if (pNumber == 7 || pNumber == 11)
     {
            purse = purse + bet;
            System.out.println("you rolled a " + pNumber);
            System.out.println("you won $" + bet );
            newBet=true; //make a new bet if you lost
     }
     else
     {
       System.out.println("you rolled a " + pNumber + ",keep rolling" );
       newBet=false; // make no new bet, neither won nor lost

     }
   } //end while


  System.out.println("Game over !")

You could add conditions to break out of the while loop if the user wants to quit before they run out of money, etc.

关键,如果要继续滚动特定数字,请将所​​有代码放入while循环或do-while循环中。

Here's a simple algorithm in plain words that may help to picture the actual code:


In a method, call it roll, use a random number generator to simulate the user's roll

If the user's roll does not equal 4,5,6,8,9, or 10, which according to your explanation are the problematic numbers, proceed with your calculations

Otherwise, if the user did in fact roll a bad number, make a recursive call to the roll method. This will generate another random roll and go through the checks again until an "acceptable" roll is generated


Not sure if you know about recursion yet, but basically, it's a method calling itself.

boolean keep_rolling = true;

While(keep_rolling== true)
{
System.out.println("How much is in your       purse?: ");
        purse = input.nextInt();
        if(purse==0){
System.out.println("you need more money to continue ");

}else{  

        System.out.println("make a bet: ");
        bet = input.nextInt();



    int pNumber = rand.nextInt(12) + 1;






        if ( pNumber == 2 || pNumber == 3 || pNumber == 12)
        {
            purse = purse - bet;
            System.out.println("you rolled a " + pNumber);
            System.out.println("you lost $" + bet);
keep_rolling= false;
        }
                    else if (pNumber == 7 || pNumber == 11)
                    {
                        purse = purse + bet;
                        System.out.println("you rolled a " + pNumber);
                        System.out.println("you won $" + bet );

        keep_rolling= false;                }
                        else
                        {

System.out.println("you rolled a " +     pNumber + ",keep rolling" );

                                        }
}//end else of purse==o
}//end while

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM