Java异常处理实验
创始人
2024-04-02 06:14:42
0

1.从键盘输入一个整形数n,如果输入正确的话,输出10-n后的值,如果输入错误的话输出“not int”最后输出end。

public class ScannerErrors {public static void main(String[] args) {Scanner sc = new Scanner(System.in);try {int m = Integer.parseInt(sc.next());System.out.println(10 - m);} catch (NumberFormatException e) {System.out.println("not int");} finally {System.out.println("end");}sc.close();}}

2.模拟向货船上装载集装箱,每个集装箱有一定重量,货船总重大于每个集装箱,装载若干集装箱后,如果货船超重,那么货船认为这是一个异常,将拒绝装载集装箱,但无论是否发生异常,货船都需要正点启航。

CargoBoat

public class CargoBoat {private int realcontent;private int maxcontent;public int getRealcontent() {return realcontent;}public void setRealcontent(int realcontent) {this.realcontent = realcontent;}public int getMaxcontent() {return maxcontent;}public void setMaxcontent(int maxcontent) {this.maxcontent = maxcontent;}public void loading(int m) throws DangerException {this.realcontent += m;if (realcontent > maxcontent) {throw new DangerException();}System.out.printf("目前装载了%d吨的货物\n", realcontent);}
}

DangerException

public class DangerException extends Exception {public void showError() {System.out.println("超重!");}
}

TestDangerException

public class TestDangerException {public static void main(String[] args) {CargoBoat cargoBoat = new CargoBoat();Scanner sc = new Scanner(System.in);cargoBoat.setMaxcontent(100);int m = 0;try {while (true) {System.out.println("请输入装载箱重量,当前装载箱重量为" + cargoBoat.getMaxcontent() + "吨");m = sc.nextInt();cargoBoat.loading(m);}} catch (DangerException e) {e.showError();System.out.printf("无法再装载重量是%d吨的集装箱\n", m);} finally {System.out.printf("启航出发!");}sc.close();}
}

在这里插入图片描述
3.写一个方法void triangle(int a,int b,int c),判断三个参数是否能构成一个三角形。如果不能则抛出异常IllegalArgumentException,显示异常信息:“a,b,c不能构成三角形”;如果可以构成则显示三角形三个边长。在主方法中得到命令行输入的三个整数,调用此方法,并捕获异常。 (注:两边之和大于第三边:a+b>c;两边之差小于第三边:a>c-a)

ErrorsTriangle

public class ErrorsTriangle {public static void triangle(int a, int b, int c) throws Exception {if (a + b > c && c - a < b) {System.out.println("能构成三角形");System.out.println("a=" + a + "," + "b=" + b + "," + "c=" + c);} else {throw new IllegalArgumentException("a=" + a + "," + "b=" + b + "," + "c=" + c + "不能构成三角形");}}public static void main(String[] args) {int a[] = new int[3];System.out.println("输入三个整数并用,分隔");Scanner sc = new Scanner(System.in);String[] strings = sc.nextLine().split(",");for (int i = 0; i < strings.length; i++) {a[i] = Integer.parseInt(strings[i]);}Arrays.sort(a);try {triangle(a[0], a[1], a[2]);} catch (IllegalArgumentException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}
}

4.编写整除运算程序,要求捕获除数为0异常、数字格式异常、通用型异常。注意要把通用型异常的捕获顺序放在最后。
5.把第4题整除程序改为双精度型实数的除法运算程序,并更改有关提示信息,运行该程序若干次,每次输入不同的数据,观察分析程序的运行结果。
6.在第4题基础上编写自定义异常类(必须继承系统的Exception类),在除数为0时抛出自定义异常,并捕获处理该异常

public class DivisibleOperation {public static void main(String[] args) {System.out.println("输入整型除数和被除数,形如a/b");Scanner sc = new Scanner(System.in);String[] strings = sc.nextLine().split("/");int a = Integer.parseInt(strings[0]);int b = Integer.parseInt(strings[1]);System.out.println(a + "/" + b + "的结果是" + divide(a, b));}public static int divide(int a, int b) {if (b == 0) {throw new ArithmeticException("除数不能为0");}return a / b;}
}

DoubleOperation

public class DoubleOperation {public static void main(String[] args) {System.out.println("输入双精度除数和被除数,形如a/b");Scanner sc = new Scanner(System.in);String[] strings = sc.nextLine().split("/");double a = Double.parseDouble(strings[0]);double b = Double.parseDouble(strings[1]);System.out.println(a + "/" + b + "的结果是" + divide(a, b));}public static double divide(double a, double b) {if (b == 0) {throw new ArithmeticException("除数不能为0");}return a / b;}
}

CustomException

public class CustomException extends Exception {public void error() {System.out.println("被除数不能为0");}
}

TestCustomException

public class TestCustomException {public static void main(String[] args) {System.out.println(divide());}public static double divide() {Scanner input = new Scanner(System.in);double a, b;for (;;) {try {System.out.println("输入第一个整数!");a = input.nextDouble();System.out.println("输入第二个整数!");b = input.nextDouble();if (b == 0) {throw new CustomException();}break;} catch (CustomException e) {e.error();System.out.println("重新输入!");}}return a / b;}
}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...