samedi 8 octobre 2016

Looping an "If Else Statement" java

I am attempting to make a simple code 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" );

                        }

Aucun commentaire:

Enregistrer un commentaire