异常
来源:互联网 发布:电子技术仿真软件 编辑:程序博客网 时间:2024/06/08 03:42
import java.util.Scanner; public class Divider {public static void main(String[] args) { int oper1 = 0; int oper2 = 0; Scanner in = new Scanner(System.in); try { System.out.print("请输入除数:"); oper1 = in.nextInt(); System.out.print("请输入被除数:"); oper2 = in.nextInt(); System.out.println("计算结果:" + oper2/oper1); } catch (ArithmeticException e1) { System.out.println("异常1:除数不能为零!"); } catch (NumberFormatException e2) { System.out.println("异常2:数字格式错误!"); } catch (Exception e3) { System.out.println("其他异常3:"+e3.getMessage()); }}}
import java.util.Scanner;public class Divider2 {public static void main(String[] args) { double oper1,oper2; Scanner in = new Scanner(System.in); System.out.print("请输入你想执行的次数:"); int n=in.nextInt(); for(int i=0;i<n;i++) { try { System.out.print("请输入除数:"); oper1 = in.nextDouble(); System.out.print("请输入被除数:"); oper2 = in.nextDouble(); double ans=oper2/oper1; System.out.println("计算结果:" + ans); } catch (ArithmeticException e1) { System.out.println("异常1:除数不能为零!"); } catch (NumberFormatException e2) { System.out.println("异常2:数字格式错误!"); } catch (Exception e3) { System.out.println("其他异常3:"+e3.getMessage()); } }}}
class MyException extends Exception{public String toString(){ return "异常1:除数等于零";}}public class Divider3{static double avg(double number1,double number2) throws MyException{double eps=1e-7;if(number2-0<=eps){throw new MyException();}return number1/number2;}public static void main(String[] args) { double oper1,oper2; Scanner in = new Scanner(System.in); System.out.print("请输入你想执行的次数:"); int n=in.nextInt(); for(int i=0;i<n;i++) { try { System.out.print("请输入除数:"); oper1 = in.nextDouble(); System.out.print("请输入被除数:"); oper2 = in.nextDouble(); double ans=avg(oper2,oper1); System.out.println("计算结果:" + ans); } catch (NumberFormatException e2) { System.out.println("异常2:数字格式错误!"); } catch (MyException e) { System.out.println(e.toString()); } catch (Exception e3) { System.out.println("其他异常3:"+e3.getMessage()); } }}}
阅读全文
0 0
- 异常
- 异常!
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- 异常
- HALCON安装与配置(VS2013+HALCON13)
- 国际化测试-ICU
- python中MySQLdb使用中踩过的坑
- ibatis中SQL映射配置文件中动态SQL语句语法
- Python的Print打印中文乱码解决方案
- 异常
- Integer 类型数据比较相等的那些坑
- 策略模式
- Android滑动冲突解决方法
- Java获取月份第一天和最后一天日期
- 用ThinkPHP框架写程序还原或搬家
- strcpy的使用需要注意dest的长度
- 推荐算法之 slope one 算法
- jxbrowser 监听所有网络请求 jxbrowser 系列教程2