第十二章第二题(InputMismatchException异常)(Inputmismatch exception)

该博客展示了如何在Java程序中处理InputMismatchException,当用户输入不符合预期(如非整数)时,程序能捕获异常并提示用户重新输入。代码示例演示了使用Scanner类从控制台读取整数并处理输入错误的情况。
摘要由CSDN通过智能技术生成

第十二章第二题(InputMismatchException异常)(Inputmismatch exception)

  • *12.2(InputMismatchException异常)编写一个程序,提示用户读取两个整数,然后显示他们的和。程序应该在输入不正确时提示用户在此读取数值。
    *12.2(Inputmismatch exception)Write a program that prompts the user to read two integers and then displays their sum. The program should prompt the user to read the value here when the input is incorrect.
  • 参考代码:
package chapter12;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Code_02 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean continueInput = true;
        do {
            try {
                System.out.print("Enter two integer: ");
                int number1 = input.nextInt();
                int number2 = input.nextInt();
                System.out.println("The nmumber enter is " + number1 + " " + number2);
                continueInput = false;
            }
            catch (InputMismatchException ex){
                System.out.println("Try again.(" + "Incorrect input:an integer is required)");
                input.nextLine();
            }
        }while (continueInput);
    }
}

  • 结果显示:
Enter two integer: ss er
Try again.(Incorrect input:an integer is required)
Enter two integer: 2  5
The nmumber enter is 2 5

Process finished with exit code 0

### 回答1: InputMismatchExceptionJava中的一种异常类型,表示在读取输入数据时遇到不匹配的类型。它通常由Scanner类在读取字符串或数字时引发。例如,如果在读取整数时遇到了字符串,则会引发InputMismatchException。 ### 回答2: 在Java编程中,InputMismatchException异常是可能发生的一种异常情况。当使用Scanner类读取用户输入时,当输入的内容与期望的输入不匹配时,就会抛出这个异常。 例如,当我们期望用户输入一个整数,但实际输入了一个字符串或小数,就会发生InputMismatchException异常。这个异常通常会在控制台输出错误信息,并且停止程序运行。 解决这个异常的办法可以通过try-catch语句捕获异常。也可以在程序代码中添加输入验证,确保用户输入的内容符合预期。比如,可以使用hasNextInt()方法判断输入是否是一个整数,或者使用正则表达式来验证输入是否符合特定的格式。 另外,在一些情况下,这个异常也可能是由于输入源被关闭而引起的。比如,输入来自于文件,但是文件被关闭了,就会发生这个异常。在这种情况下,我们需要确保输入源确实存在并且可用。在处理完异常后,我们需要及时关闭输入流,防止资源泄漏。 总之,InputMismatchException异常Java编程中是一个常见的异常情况,需要我们注意处理。通过加强输入验证,避免输入不符合预期的情况,可以有效地防止这个异常的发生。 ### 回答3: InputMismatchException异常Java中的一种异常类型,表示输入不匹配。当使用Scanner类读取一个错误类型的输入时,就会抛出InputMismatchException异常。 举个例子:如果使用Scanner读取一个int类型的数,但是输入的是一个String类型的字符串,就会抛出InputMismatchException异常。 下面是一个示例代码,当用户输入的不是int类型的数时,就会抛出InputMismatchException异常: ``` import java.util.Scanner; public class InputMismatchExceptionDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please input a number:"); try { int num = scanner.nextInt(); System.out.println("The number is: " + num); } catch (InputMismatchException e) { System.out.println("Input error, please input an integer."); } } } ``` 当用户输入一个非int类型的字符时,就会抛出InputMismatchException异常,程序就会执行catch块中的代码,输出"Input error, please input an integer."。 在使用Scanner类读取输入数据时,我们应该避免输入错误类型的数据,否则就会抛出InputMismatchException异常。如果要读取不同类型的数据,可以使用不同的next方法,如nextBoolean、nextDouble、nextLine等,这样可以有效避免InputMismatchException异常的出现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值