本文旨在帮助开发者解决在Java Craps游戏中遇到的循环中断问题。通过分析代码,我们将定位问题所在,并提供修改方案,确保游戏在玩家输入“n”或余额低于10美元时能够正常结束,避免抛出异常。同时,我们将优化代码中的格式化输出,使其更加简洁易懂。
解决循环中断问题
原始代码中,循环的中断逻辑存在一些潜在的问题,导致程序在特定情况下无法正常结束循环。主要问题集中在以下两个方面:
- 余额不足时的中断: 当玩家的余额低于10美元时,循环应该中断,但原始代码中可能存在逻辑错误,导致程序无法正确识别并执行中断操作。
- 玩家输入“n”时的中断: 当玩家输入“n”或“N”表示不想继续游戏时,循环也应该中断,但原始代码可能存在判断错误,导致程序无法正确识别并执行中断操作。
为了解决这些问题,我们需要仔细检查循环条件和中断逻辑,确保它们能够正确地处理各种情况。
修改方案
- 检查循环条件: 确保while循环的条件能够正确地判断游戏是否应该继续。原始代码中的循环条件是playAgain && total > 0。虽然这个条件看起来合理,但我们需要确保playAgain变量的值能够正确地反映玩家的意愿。
- 优化中断逻辑: 在循环体内部,我们需要添加明确的中断语句,以便在满足特定条件时能够立即结束循环。例如,当玩家的余额低于10美元时,我们可以使用break语句来中断循环。同样,当玩家输入“n”时,我们也可以使用break语句来中断循环。
以下是修改后的代码片段,其中包含了优化的中断逻辑:
立即学习“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("无效的输入,请重新输入:"); } }
注意事项:
- 确保在读取玩家输入之前,先使用in.nextLine()来消耗掉换行符。这是因为in.nextInt()方法只会读取整数,而不会读取换行符,这可能导致后续的in.nextLine()方法读取到的是一个空字符串。
- 使用equalsIgnoreCase()方法来比较字符串,这样可以忽略大小写,提高代码的健壮性。
优化格式化输出
原始代码中使用了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编程。
评论(已关闭)
评论已关闭