java 异常处理
Posted fitzroy343
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 异常处理相关的知识,希望对你有一定的参考价值。
参考:
海伦公式
开根号 Math.sqrt
mport java.util.*; public class test { public static void main(String[] args){ try { Scanner sc = new Scanner(System.in); System.out.println("请输入加法加法表达式,格式为:21+12+31"); String s = sc.nextLine(); String[] num = s.split("\\+"); int num_1 = Integer.parseInt(num[0]); int num_2 = Integer.parseInt(num[1]); int num_3 = Integer.parseInt(num[2]); int result = num_1+num_2+num_3; System.out.println(num_1 + "+" + num_2 + "+" + num_3 + "=" +result); }catch(Exception e) { System.out.println("Input Error!!"); } } }
public class Triangle { double a; double b; double c; public Triangle( double a, double b, double c ){ this.a = a; this.b = b; this.c = c; } void isTriangle() throws MyException{ if( a+b<=c || a+c<=b || b+c<=a ){ throw new MyException("无法构成三角形,两边之和小于第三边"); } else{ System.out.println("构成三角形"); } } public static void main(String[] args){ Triangle t = new Triangle(2,2,5); try{ t.isTriangle(); }catch(MyException e){ System.out.println(e.getMessage()); }catch(Exception e){ System.out.println("程序发生了其他的异常"); } } } class MyException extends Exception{ String message; public MyException(String ErrorMessage){ message = ErrorMessage; } public String getMessage(){ return message; } }
import java.lang.Math; import java.util.*; abstract class Shape{ abstract protected void getArea(); } class triangle extends Shape{ double a; double b; double c; triangle(double a, double b, double c){ this.a = a; this.b = b; this.c = c; } public void getArea(){ double p = a+b+c/2; System.out.println("三角形面积:" +String.format("%.2f",Math.sqrt(p*(p-a)*(p-b)*(p-c)))); } } class circle extends Shape{ double r; circle(double r){ this.r = r; } public void getArea(){ System.out.println("圆形面积:"+String.format("%.2f",Math.PI*r*r)); } } class rectangle extends Shape{ double x; double y; rectangle( double x, double y ){ this.x = x; this.y = y; } public void getArea(){ System.out.println("矩形面积:"+String.format("%.2f",x*y)); } } class mException extends Exception{ String message; public mException(String ErrorMessage){ message = ErrorMessage; } public String getMessage(){ return message; } } public class Test_3 { triangle t = new triangle(3,4,5); rectangle r = new rectangle(3,4); circle c = new circle(5); void cmd(String s) throws mException{ if( s.equals("1") || s.equals("2") || s.equals("3") ) { switch (s) { case "1": c.getArea(); break; case "2": r.getArea(); break; case "3": t.getArea(); break; } } else{ System.out.println("输入错误"); } } public static void main(String[] args){ Scanner sc = new Scanner(System.in); String s = sc.nextLine(); Test_3 t = new Test_3(); try{ t.cmd(s); }catch(mException e){ System.out.println(e.getMessage()); }catch(Exception e){ System.out.println("程序其他错误"); } } }
以上是关于java 异常处理的主要内容,如果未能解决你的问题,请参考以下文章
java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理(代码片段
java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.database.sqlite异常(代码片段
PCL异常处理:pcl 1.8.13rdpartyoostincludeoost-1_64oost ypeofmsvc ypeof_impl.hpp(125): error(代码片段