猿问

当用户输入字符串而不是整数输入时捕获错误

我希望代码在用户输入字符串而不是整数时捕获错误。您可以看到我尝试了一个仍然无法正常工作的 try catch 块。除此之外,其他一切都很完美。我该如何解决这个问题?


这是输出应该如何:


Welcome to the Squares and Cubes table


Enter an integer: five

Error! Invalid integer. Try again.

Enter an integer: -5

Error! Number must be greater than 0

Enter an integer: 101

Error! Number must be less than or equal to 100

Enter an integer: 9


Number  Squared Cubed

======  ======= =====

1       1       1

2       4       8

3       9       27

4       16      64

5       25      125

6       36      216

7       49      343

8       64      512

9       81      729


Continue? (y/n): y


Enter an integer: 3


Number  Squared Cubed

======  ======= =====

1       1       1

2       4       8

3       9       27


不负相思意
浏览 174回答 3
3回答

米脂

我稍微更改了您的代码并将其作为一个整体发布,以避免混淆:public static void main(String[] args) {&nbsp; &nbsp; // Welcome the user&nbsp; &nbsp; System.out.println("Welcome to the Squares and Cubes table");&nbsp; &nbsp; System.out.println();&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; String choice = "y";&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; int integer = Integer.MAX_VALUE;&nbsp; &nbsp; &nbsp; &nbsp; while (integer == Integer.MAX_VALUE) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get input from the user&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter an integer: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String input = sc.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer = Integer.parseInt(input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (NumberFormatException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error! Invalid integer. Try again.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(integer<0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error! Number must be greater than 0");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter an integer: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(integer>100){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error! Number must be less than or equal to 100");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter an integer: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; integer = sc.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Create a header&nbsp; &nbsp; &nbsp; &nbsp; String header = "Number&nbsp; " + "Squared " + "Cubed&nbsp; &nbsp;" + "\n"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +&nbsp; &nbsp;"======&nbsp; " + "======= " + "=====&nbsp; &nbsp;";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(header);&nbsp; &nbsp; &nbsp; &nbsp; int square = 0;&nbsp; &nbsp; &nbsp; &nbsp; int cube = 0;&nbsp; &nbsp; &nbsp; &nbsp; String row = "";&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= integer; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; square = i * i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cube = i * i * i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row = i + "&nbsp; &nbsp; &nbsp; &nbsp;" + square + "&nbsp; &nbsp; &nbsp; &nbsp;" + cube;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(row);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // See if the user wants to continue&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Continue? (y/n): ");&nbsp; &nbsp; &nbsp; &nbsp; choice = sc.next();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; } while (!choice.equalsIgnoreCase("n"));}这个想法是while在你的循环中创建另一个并运行它直到用户传递一个整数。

三国纷争

Integer.parseInt方法是将 the 转换String为 int 并NumberFormatException在字符串无法转换为int类型时抛出 a 。应该是这样的:&nbsp; &nbsp; System.out.print("Enter an integer: ");&nbsp; &nbsp; Scanner sc =new Scanner(System.in);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; int integer = Integer.parseInt(sc.nextLine());&nbsp; &nbsp; } catch (NumberFormatException e) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error! Invalid integer. Try again.");&nbsp; &nbsp; }

慕运维8079593

您可以使用此方法来测试输入的值是否为有效整数。以此结果为基础,您可以从其他验证开始public boolean isInt(string input) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; Integer.parseInt(text);&nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; } catch (NumberFormatException e) {&nbsp; &nbsp; &nbsp;return false;&nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答