
本文旨在帮助开发者解决在Java Craps游戏中遇到的循环中断问题。通过分析代码,我们将定位问题所在,并提供修改方案,确保游戏在玩家输入“n”或余额低于10美元时能够正常结束,避免抛出异常。同时,我们将优化代码中的格式化输出,使其更加简洁易懂。
原始代码中,循环的中断逻辑存在一些潜在的问题,导致程序在特定情况下无法正常结束循环。主要问题集中在以下两个方面:
为了解决这些问题,我们需要仔细检查循环条件和中断逻辑,确保它们能够正确地处理各种情况。
以下是修改后的代码片段,其中包含了优化的中断逻辑:
立即学习“Java免费学习笔记(深入)”;
while (playAgain && total > 0) {
// 游戏逻辑
if (total <= 9) {
System.out.println("您的余额不足,游戏结束!");
break; // 余额不足,中断循环
}
System.out.println("继续游戏 (y/Y or n/N)? ");
in.nextLine(); // Consume the newline character
String again = in.nextLine();
if (again.equalsIgnoreCase("n")) {
playAgain = false;
break; // 玩家选择不继续游戏,中断循环
} else if (!again.equalsIgnoreCase("y")) {
System.out.println("无效的输入,请重新输入:");
}
}注意事项:
原始代码中使用了System.out.printf()方法来进行格式化输出,但在某些情况下,这种方式可能会导致异常。为了避免这种情况,我们可以使用字符串连接的方式来进行格式化输出。
将以下代码:
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 + "%.");这种方式更加简洁易懂,并且可以避免由于格式化字符串错误而导致的异常。
以下是修改后的完整示例代码:
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) {
System.out.println("您的余额不足,游戏结束!");
break; // 余额不足,中断循环
}
System.out.println("继续游戏 (y/Y or n/N)? ");
in.nextLine(); // Consume the newline character
String again = in.nextLine();
if (again.equalsIgnoreCase("n")) {
playAgain = false;
break; // 玩家选择不继续游戏,中断循环
} else if (!again.equalsIgnoreCase("y")) {
System.out.println("无效的输入,请重新输入:");
}
}// 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.println("Based on your play, the probability of winning is " + 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通过本文的讲解,我们解决了Java Craps游戏中遇到的循环中断问题,并优化了代码中的格式化输出。通过仔细检查循环条件和中断逻辑,我们可以确保游戏在各种情况下都能够正常结束。同时,通过使用字符串连接的方式来进行格式化输出,我们可以避免由于格式化字符串错误而导致的异常。希望本文能够帮助您更好地理解和掌握Java编程。
以上就是修复Java Craps游戏中的循环中断异常的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号