gpt4 book ai didi

java - 尝试将字符串解析为 int 时出错

转载 作者:行者123 更新时间:2023-11-30 04:27:10 25 4
gpt4 key购买 nike

在这个 Java 程序中,用户应该猜测 1 到 100 之间的数字,然后如果您按 S,它会显示尝试的摘要。问题是我正在获取输入字符串并将其转换为数字,以便我可以将其与范围进行比较,但随后我还需要能够使用该字符串作为菜单输入。更新在用户猜测正确后,如何使程序返回到菜单选项。因此,在用户获胜后,我希望问题能够显示摘要报告,该报告可以通过使用 S

进行访问

这是我的代码

public class GuessingGame {
public static void main(String[] args) {


// Display list of commands
System.out.println("*************************");
System.out.println("The Guessing Game-inator");
System.out.println("*************************");
System.out.println("Your opponent has guessed a number!");
System.out.println("Enter a NUMBER at the prompt to guess.");
System.out.println("Enter [S] at the prompt to display the summary report.");
System.out.println("Enter [Q] at the prompt to Quit.");
System.out.print("> ");


// Read and execute commands
while (true) {

// Prompt user to enter a command
SimpleIO.prompt("Enter command (NUMBER, S, or Q): ");
String command = SimpleIO.readLine().trim();

// Determine whether command is "E", "S", "Q", or
// illegal; execute command if legal.
int tries = 0;
int round = 0;
int randomInt = 0;
int number = Integer.parseInt(command);
if (number >= 0 && number <= 100) {
if(randomInt == number){

System.out.println("Congratulations! You have guessed correctly." +
" Summary below");
round++;
}
else if(randomInt < number)
{
System.out.println("your guess is TOO HIGH. Guess again or enter Q to Quit");
tries++;
}
else if(randomInt > number){
System.out.println("your guess is TOO LOW. Guess again or enter Q to Quit");
tries++;
}

} else if (command.equalsIgnoreCase("s")) {
// System.out.println("Round Guesses");
// System.out.println("-------------------------");
// System.out.println(round + "" + tries);



} else if (command.equalsIgnoreCase("q")) {
// Command is "q". Terminate program.
return;

} else {
// Command is illegal. Display error message.
System.out.println("Command was not recognized; " +
"please enter only E, S, or q.");
}

System.out.println();
}
}
}

最佳答案

您应该首先检查 S/Q 值,然后将字符串解析为整数。如果您捕获 NumberFormatException(由 Integer.parseInt() 引发),您可以确定输入是否为有效值。我会做类似的事情:

if ("s".equalsIgnoreCase(command)) {
// Print summary
} else if ("q".equalsIgnoreCase(command)) {
// Command is "q". Terminate program.
return;
} else {
try {
Integer number = Integer.parseInt(command);
if(number < 0 || number > 100){
System.out.println("Please provide a value between 0 and 100");
} else if(randomInt == number){
System.out.println("Congratulations! You have guessed correctly." +
" Summary below");
round++;
} else if(randomInt < number) {
System.out.println("your guess is TOO HIGH. Guess again or enter Q to Quit");
tries++;
} else if(randomInt > number) {
System.out.println("your guess is TOO LOW. Guess again or enter Q to Quit");
tries++;
}
} catch (NumberFormatException nfe) {
// Command is illegal. Display error message.
System.out.println("Command was not recognized; " +
"please enter only a number, S, or q.");
}
}

使用这个算法(我确信它可以优化),您可以处理以下情况:

  • 用户输入 s/S
  • 用户输入 q/Q
  • 用户输入了无效值(不是数字)
  • 用户输入了无效数字(小于 0 或大于 100)
  • 用户输入有效号码

关于java - 尝试将字符串解析为 int 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15591833/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com