Java perfects the error message of division operation
•
Java
public class Compute { private int[] num = new int[2]; public int[] getNum() { return num; } public void setNum(int[] num) { this.num = num; } }
public class Test04 { public static void main(String[] args) { Compute c = new Compute(); int array[] = c.getNum(); int res = 0; String YorN = null; Scanner in = new Scanner(system.in); try { System.out.println("请输入第一个整数:"); array[0] = in.nextInt(); System.out.println("请输入第二个整数:"); array[1] = in.nextInt(); res = array[0] / array[1]; System.out.println("是否保存结果请输入Y或者N"); YorN = in.next(); if (YorN.equals("Y")) { array[2] = res; } System.out.println(array[0] + "除以" + array[1] + "的结果是:" + res); } } }
catch (Arrayindexoutofboundsexception e) { System.out.println("出现数组越界错误,下标过大或者过小。"); } catch(ArithmeticException e) { System.out.println("出现算术运算错误,被除数不能为0。"); } catch(InputMismatchException e) { System.out.println("输入的数据类型不匹配,只能输入数字。"); } catch(Exception e) { System.out.println("发生未知错误。"); }
请输入第一个整数: 100 请输入第二个整数: 0 出现算术运算错误,被除数不能为0。
请输入第一个整数: 100 请输入第二个整数: 9t 输入的数据类型不匹配,只能输入数字。
请输入第一个整数: 100 请输入第二个整数: 10 是否保存结果请输入Y或者N Y 出现数组越界错误,下标过大或者过小。
请输入第一个整数: 100 请输入第二个整数: 20 是否保存结果请输入Y或者N N 100除以20的结果是:5
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码