本题目要求读入2个整数a和b,然后输出它们的商 ,即a/b的值,正常输出结果并保留2位小数,如果出现输入非数值型数据,则捕获异常NameError,并输出the input is not numerical,如果输入的除数b为0,则捕获异常ZeroDivisionError,输出zero division error
输入格式:
在2行中输入2个数据。
输出格式:
在一行中输出a/b的值,保留2位小数,若存在异常则捕获输出相应信息。
输入样例1:
在这里给出一组输入。例如:
1
2
输出样例1:
在这里给出相应的输出。例如:
0.50
输入样例2:
在这里给出一组输入。例如:
2
b
输出样例2:
在这里给出相应的输出。例如:
the input is not numerical
输入样例3:
在这里给出一组输入。例如:
4
0
输出样例3:
在这里给出相应的输出。例如:
zero division error
代码:
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int a = 0, b = 0;
try {
a = in.nextInt();
b = in.nextInt();
} catch (Exception e) {
System.out.println("the input is not numerical");
System.exit(0);
}
if (b == 0) {
System.out.println("zero division error");
System.exit(0);
}
System.out.printf("%.2f",a*1.0/b);
}
}