我試圖弄清楚為什么如果為 playAgain 輸入“n”或者當總額低于 10 美元時,我無法打破回圈。如果您看到我如何打破回圈以運行 gameOver 函式而不會引發例外,那將是一個很大的幫助。我在下面的代碼中注意到我遇到了麻煩。我不確定為什么會拋出這個例外。如果您能夠在總數小于 10 或 playAgain 為假時找出如何打破回圈,請告訴我!
import java.util.Random;
import java.util.Scanner;
public class GameOfCrapsTester {
static Scanner in = new Scanner(System.in);
static Random rand = new Random();
public static void main(String[] args) {
System.out.println("Welcome to the game of Craps");
System.out.println(" ");
System.out.println("The house has given you a starting balance of $500");
System.out.println("On each round, you will make a whole number wager.");
System.out.println("The minimum wager is $10, and the maximum wager is your remaining balance.");
System.out.println(" ");
System.out.println("You may keep playing until you decide to cash in, or");
System.out.println(" you can't cover the minimum wager.");
System.out.println("Good Luck!");
boolean win;
double wins = 0, numOfGames = 0;
int total = 500;
// Come out roll and set point value
int pointValue = 0;
boolean playAgain = true;
while (playAgain && total > 0)
{
System.out.println(" ");
System.out.println("Your balance is $" total);
System.out.println(" ");
System.out.println("Place your bet: $");
// Get and check wager placed
int bet = in.nextInt();
while (bet > total || bet < 10)
{
if (bet < 10)
{
System.out.println("Bet must be larger than $10.");
}
System.out.println("I'm sorry, that's not a valid wager; please re-enter: ");
bet = in.nextInt();
}
int num = rollDice();
if ((num >= 4 && num <= 10 && num != 7) || num == 0)
{
pointValue = num;
System.out.println(" ");
System.out.println("Your point value is " pointValue);
System.out.println(" ");
win = rollWithPoint(pointValue);
if (win)
{
total = wonGame(bet, total);
wins ;
numOfGames ;
System.out.println("Wins: " wins " Number of games: " numOfGames);
}
else if (!win)
{
total = lostGame(bet, total);
numOfGames ;
System.out.println("Wins: " wins " Number of games: " numOfGames);
}
}
else if (num == 7 || num == 11)
{
total = wonGame(bet, total);
wins ;
numOfGames ;
System.out.println("Wins: " wins " Number of games: " numOfGames);
}
else
{
total = lostGame(bet, total);
numOfGames ;
System.out.println("Wins: " wins " Number of games: " numOfGames);
}
if (total <= 9) // THIS IS WHERE I AM HAVING TROUBLE
{
break;
}
System.out.println("Keep playing (y/Y or n/N)? ");
in.nextLine();
String again = in.nextLine();
if (again.equalsIgnoreCase("y"))
{
playAgain = true;
}
else if (again.equalsIgnoreCase("n")) // THIS IS WHERE I AM HAVING TROUBLE
{
break;
}
else
{
System.out.println("Invalid character input, try again:");
again = in.nextLine();
}
}// end of loop
gameOver(wins, numOfGames);
} // END of main
public static int rollDice() {
int dice1, dice2, total;
dice1 = rand.nextInt(6) 1;
dice2 = rand.nextInt(6) 1;
total = dice1 dice2;
System.out.print("Your roll: ");
System.out.print("Dice1: " dice1);
System.out.print(", Dice2: " dice2);
System.out.println("; Roll Value: " total);
return total;
} // END of rollDice
public static boolean rollWithPoint(int point) {
int total = rollDice();
boolean winner = false;
while(total != 7 && winner == false)
{
total = rollDice();
if (total == point)
{
winner = true;
}
else
{
winner = false;
}
}
return winner;
} // END of rollWithPoint
public static int lostGame(int bet, int total) {
System.out.println("Oh, I'm sorry, you lost.");
System.out.println(" ");
total = total - bet;
System.out.println("Your current balance: $" total);
System.out.println(" ");
return total;
} // END of lostGame
public static int wonGame(int bet, int total) {
System.out.println("A winner!");
System.out.println(" ");
total = total bet;
System.out.println("Your current balance: $" total);
System.out.println(" ");
return total;
} // END of wonGame
public static void gameOver(double win, double tot) {
double winPercent = (win / tot) * 100;
System.out.println(" ");
System.out.printf("Based on your play, the probability of winning is %.2%", winPercent);
System.out.println(" ");
System.out.println("Seems you lost your shirt; better luck next time.");
System.out.println("Have a nice day! Hope to see you soon!");
} // END of gameOver
} // END of GameOfCraps
uj5u.com熱心網友回復:
更改時沒有錯誤(不使用 String.format()):
System.out.printf("Based on your play, the probability of winning is %.2%", winPercent);
對此:
System.out.println("Based on your play, the probability of winning is " winPercent "%.");
小賭注示例(控制臺):
Your balance is $11
Place your bet: $
10
Your roll: Dice1: 1, Dice2: 2; Roll Value: 3
Oh, I'm sorry, you lost.
Your current balance: $1
Wins: 0.0 Number of games: 2.0
Based on your play, the probability of winning is 0.0%.
Seems you lost your shirt; better luck next time.
Have a nice day! Hope to see you soon!
如果為 playAgain 輸入“n”或當總數低于 10 美元時,我無法打破回圈。
它也很好用。如果我下注低于 10,它會要求我再下注。什么
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/528590.html
