问题
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.
The code will ask user to input a bet.
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" );
}
回答1:
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.
回答2:
Key, if you want to keep rolling for the specific numbers put all your code inside a while-loop or do-while loop.
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/39937994/looping-an-if-else-statement-java