您的位置:首页 > 编程语言 > Java开发

Introduction to Java Programming编程题12.2<InputMismatchException>

2015-09-28 19:14 489 查看
运行结果:

Enter two digital number: tr ru hd
Invalid digital number.
Enter two digital number: 45 qa
Invalid digital number.
Enter two digital number: 12 11
12 + 11 = 23


InputException.java

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

public class InputException {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
do {
System.out.print("Enter two digital number: ");

try {
int a = input.nextInt();
int b = input.nextInt();
int sum = a + b;
System.out.println(a + " + " + b + " = " + sum);
break;
}
catch (InputMismatchException ex) {
System.out.println("Invalid digital number.");
input.nextLine();
continue;
}
} while (true);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  异常处理