在下面的程序中,我创建了一个简单的数字游戏,选择 1 到 100 之间的数字并要求用户猜测该数字。用户可以继续猜测,直到尝试了 10 次为止。
当我运行程序并输入字符串时,程序抛出 NumberFormatException。单击“确定”后,我找不到处理此问题的方法,因此我调用了“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");
}
}
}
}
这是您的代码,正在执行您想要的操作(可能不是最有效的方法)
public class NumberGame {
static int scoreOfWins = 0;
static int numberOfAttempt = 0;
public static void main(String[] args) {
game();
}
static int num = new Random().nextInt(100) + 1;
static int count = 10;
static void game() {
try {
while(count > 0) {
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!");
game();
}
}
static void restart() {
count = 0;
game();
}
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.equalsIgnoreCase("yes")) {
restart();
} else if (inputAns.equals("N") |inputAns.equalsIgnoreCase("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");
}
}
}
}
所以我在你的代码中修复的第一件事是一个小但很好的练习,那就是命名约定。这是非常不言自明的。
其次,我使用了
inputAns.equalsIgnoreCase("yes")
和 inputAns.equalsIgnoreCase("no")
,它允许用户以他们想要的任何方式输入“yes”,甚至是“Yes”。
接下来我实现了
new Random().nextInt(100) + 1
,这可能是生成随机数的更好方法。
除了从游戏方法中删除
num
和 count
并将其设为一个字段之外,我通常没有更改您的很多代码。现在,每次运行游戏方法时,计数都不会改变,只有当您重新启动游戏时才会改变。