gpt4 book ai didi

java - 如果 Scanner.nextInt() 无效,则显示 else block 中的错误输入

转载 作者:行者123 更新时间:2023-12-01 22:12:27 25 4
gpt4 key购买 nike

我知道有很多代码,但在为扫描仪定义速度后,重点主要集中在 block 中的空白 else 空间内。如果用户没有输入整数,我希望 else 打印代码循环,并说“再试一次!输入无效。” ETC;但我不知道该去哪里。我听说过“catch”和“Exception”,但是有人可以向我解释一下,或者为我发布一个更新的代码块并进行解释。谢谢!

import java.util.Scanner;

public class M2A1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String Birthday;
int Bday = 2;
int speed;
int penalty;
System.out.println("You were pulled by a police officer believing you were speeding in a 55mph zone.");
System.out.println("You hand over your license and registration. Is it possibly your birthday today?");
System.out.println("Use yes(y) or no(n) to answer the above!");
Birthday = input.nextLine();
if (Birthday.equalsIgnoreCase("yes") || Birthday.equalsIgnoreCase("y"))
{
System.out.println("Great, your bad driving may have happened on a good day.");
Bday = 1;
}
else if (Birthday.equalsIgnoreCase("n") || Birthday.equalsIgnoreCase("no"))
{
System.out.println("That's ok. Maybe you drove under the speed limit.");
Bday = 0;
}
else
{
while (!(Birthday.equalsIgnoreCase("y") || Birthday.equalsIgnoreCase("yes") || Birthday.equalsIgnoreCase("n") || Birthday.equalsIgnoreCase("no"))) {
System.out.println("Invalid response. Try again!");
System.out.println("Use yes(y) or no(n) to answer the above!");
Birthday = input.nextLine();
if (Birthday.equalsIgnoreCase("y") || Birthday.equalsIgnoreCase("yes"))
{
System.out.println("Great, your bad driving may have happened on a good day.");
}
else if (Birthday.equalsIgnoreCase("n") || Birthday.equalsIgnoreCase("no"))
{
System.out.println("That's ok.");
}
}
}
System.out.println("How fast were you going?");
speed = input.nextInt();

if (Bday == 1) {
if (speed <= 65)
{
penalty = 0;
System.out.println("Great job driving, and the officer wishes you a happy birthday! You drove at " +speed+ "mph! You lost " +penalty+ " points from your license.");
}
else if (speed > 65 && speed <= 85)
{
penalty = 1;
System.out.println("You are driving a little fast, but the officer wishes you a happy birthday! You drove at " +speed+ "mph! You lost " +penalty+ " points from your license.");
}
else if (speed > 85)
{
penalty = 2;
System.out.println("You are driving way too fast, even for your birthday. You drove at " +speed+ "mph! You lost " +penalty+ " points from your license.");
}
else
{

}
}
if (Bday == 0) {
if (speed <= 60)
{
penalty = 0;
System.out.println("Great job driving! You drove at " +speed+ "mph! You lost " +penalty+ " points from your license.");
}
else if (speed > 60 && speed <= 80)
{
penalty = 1;
System.out.println("You are driving a little fast! You drove at " +speed+ "mph! You lost " +penalty+ " points from your license.");
}
else if (speed > 80)
{
penalty = 2;
System.out.println("You are driving WAY too fast! You drove at " +speed+ "mph! You lost " +penalty+ " points from your license.");
}
else
{

}
}
}
}

最佳答案

扫描仪实际上有一个 hasNextInt() 方法,这可能就是您正在寻找的方法。

在您的示例中使用此方法的示例可能类似于:

while(!input.hasNextInt()){
System.out.println("Try again! Invalid Input");
input.nextLine(); //This clears the buffer and will allow the user to enter again.
}
speed = input.nextInt();

关于java - 如果 Scanner.nextInt() 无效,则显示 else block 中的错误输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58648705/

25 4 0
文章推荐: java - 如何使用 Selenium Webdriver 访问其 xpath 发生变化的 WebElement
文章推荐: java - 扫描仪 next() 输入不匹配异常 ..在 nextInt() 之后使用 nextLine()
文章推荐: java - Spring MVC 在类 'xxxx' 中找不到属性 'xxxx' 的 setter
文章推荐: java - 在 Spring Boot Controller 中反序列化 json 时 List null