Java猜数字游戏异常后如何从当前剩余尝试次数恢复运行
猜数字游戏异常处理优化问题
我编写了一个简单的猜数字游戏:程序随机生成1到100之间的数字,用户最多可进行10次猜测尝试。当前程序存在一个问题:当用户输入字符串时会抛出NumberFormatException,点击弹窗的“OK”后,目前只能调用TryAgain方法重新开始游戏,但我希望实现从当前剩余的尝试次数处继续运行程序,而非重新开始。
原代码
import javax.swing.JOptionPane; public class NumberGame { static int scoreOfWins = 0; static int numberOfAttempt = 0; public static void main(String[] args) { Game(); } static void Game() { try { int num = (int) (Math.random() * 100); int count = 10; while(count > 0) { // System.out.println(num); String inputTitle = "Guess the number between 1 to 100"; String input = JOptionPane.showInputDialog(inputTitle, "Enter your answer here..."); if (input == null) { System.exit(0); } int intInput = Integer.parseInt(input); System.out.println(intInput); if (intInput == num) { numberOfAttempt = 11 - count; JOptionPane.showMessageDialog(null, "You win! Your guess was right after " + numberOfAttempt + " attempts"); scoreOfWins++; TryAgain(); return; } else if (intInput > num) { String numberOfTimes = " You can try " + (count - 1) + " times"; JOptionPane.showMessageDialog(null, "Your guess was not right! Please try Again with a lower value." + numberOfTimes); count--; } else if (intInput < num){ String numberOfTimes = " You can try " + (count - 1) + " times"; JOptionPane.showMessageDialog(null, "Your guess was not right! Please try Again with a higher value." + numberOfTimes); count--; } } JOptionPane.showMessageDialog(null, "You Lost the game!"); TryAgain(); } catch (Exception E) { JOptionPane.showMessageDialog(null, "Number format exception detected, Please only input numbers!"); TryAgain(); } } static void TryAgain() { while (true) { String inputAns = JOptionPane.showInputDialog("Type yes if you want to containue!" + " Type No if you want to give up!", "you won " + scoreOfWins + " times do you want to continue?"); if (inputAns == null) { System.exit(0); } inputAns = inputAns.toUpperCase(); if (inputAns.equals("Y") | inputAns.equals("Yes")) { Game(); } else if (inputAns.equals("N") |inputAns.equals("NO")) { JOptionPane.showMessageDialog(null, "OKAY you won " + scoreOfWins + " times today, see you later!"); return; } else { JOptionPane.showMessageDialog(null, "Please provide a good valid identifier like Yes or NO"); } } } }
解决方案
问题核心是你把整个游戏逻辑包裹在了外层try-catch中,触发异常后直接跳出循环并重启游戏。要实现从剩余次数继续,需将异常捕获范围缩小到仅输入解析环节,确保异常不会中断整个循环:
- 将
Integer.parseInt(input)单独放入try-catch块 - 捕获到NumberFormatException时,仅提示错误,用
continue跳过当前循环,保留剩余尝试次数
修改后的代码
import javax.swing.JOptionPane; public class NumberGame { static int scoreOfWins = 0; static int numberOfAttempt = 0; public static void main(String[] args) { Game(); } static void Game() { int num = (int) (Math.random() * 100); int count = 10; while(count > 0) { // System.out.println(num); String inputTitle = "Guess the number between 1 to 100"; String input = JOptionPane.showInputDialog(inputTitle, "Enter your answer here..."); if (input == null) { System.exit(0); } // 单独捕获输入解析异常 int intInput; try { intInput = Integer.parseInt(input); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "Number format exception detected, Please only input numbers!"); continue; // 跳过当前循环,次数不减少 } System.out.println(intInput); if (intInput == num) { numberOfAttempt = 11 - count; JOptionPane.showMessageDialog(null, "You win! Your guess was right after " + numberOfAttempt + " attempts"); scoreOfWins++; TryAgain(); return; } else if (intInput > num) { String numberOfTimes = " You can try " + (count - 1) + " times"; JOptionPane.showMessageDialog(null, "Your guess was not right! Please try Again with a lower value." + numberOfTimes); count--; } else if (intInput < num){ String numberOfTimes = " You can try " + (count - 1) + " times"; JOptionPane.showMessageDialog(null, "Your guess was not right! Please try Again with a higher value." + numberOfTimes); count--; } } JOptionPane.showMessageDialog(null, "You Lost the game!"); TryAgain(); } static void TryAgain() { while (true) { String inputAns = JOptionPane.showInputDialog("Type yes if you want to continue!" + " Type No if you want to give up!", "you won " + scoreOfWins + " times do you want to continue?"); if (inputAns == null) { System.exit(0); } inputAns = inputAns.toUpperCase(); // 替换为短路或运算符,修正拼写错误 if (inputAns.equals("Y") || inputAns.equals("YES")) { Game(); } else if (inputAns.equals("N") || inputAns.equals("NO")) { JOptionPane.showMessageDialog(null, "OKAY you won " + scoreOfWins + " times today, see you later!"); return; } else { JOptionPane.showMessageDialog(null, "Please provide a valid input like Yes or NO"); } } } }
额外优化:
- 将
|替换为短路或||,符合Java常规写法 - 修正TryAgain方法中的拼写错误
containue为continue - 捕获具体的
NumberFormatException而非泛型Exception,避免误捕无关异常
内容的提问来源于stack exchange,提问作者Wahid Ahmad Rustaqi
相关产品推荐
相关产品推荐

