1 /* 2 编写程序DivideExcep.java。从控制台输入两个数,计算两数相除并输出结果。 3 使用两个catch子句,分别捕捉除数为0的异常和参数输入有误异常。 4 编译并运行,当输入除数为0时,将有异常出现,当输入的不是整数时, 5 如将30输成了3o,出现的是另一种异常。(参数输入非数字的异常请自行查阅JDK API) 6 注:除数为0的异常:ArithmeticException 7 输入格式的异常:NumberFormatException 8 */ 9 import java.util.InputMismatchException; 10 import java.util.Scanner; 11 public class DivideExcep { 12 public static void main(String[] args) { 13 Scanner input=new Scanner(System.in); 14 15 try{ 16 System.out.println("请输入被除数:"); 17 int num1=input.nextInt(); 18 System.out.println("请输入除数:"); 19 int num2=input.nextInt(); 20 System.out.println(num1/num2); 21 }catch(ArithmeticException e){ 22 System.out.println("输入的除数为0,错误"); 23 }catch(InputMismatchException e){ 24 System.out.println("参数输入的非数字"); 25 } 26 } 27 }
1 package Javashiyan9d; 2 /* 3 判断结果输出什么 4 注意:err输出为随机 5 */ 6 public class J_Test { 7 public static void mb_createException(){ 8 throw new ArrayIndexOutOfBoundsException(); 9 } 10 public static void mb_method(){ 11 try{ 12 mb_createException(); 13 System.out.println("a"); 14 }catch (ArithmeticException e){ 15 System.err.println("b"); 16 }finally { 17 System.out.println("c"); 18 } 19 System.out.println("d"); 20 } 21 22 public static void main(String[] args) { 23 try{ 24 mb_method(); 25 }catch (Exception e){ 26 System.err.println('m'); 27 } 28 System.out.println('n'); 29 } 30 31 32 }
1 package Javashiyan9c; 2 3 public class NotTriangleException extends RuntimeException{ 4 public NotTriangleException() { 5 } 6 7 public NotTriangleException(String message) { 8 super(message); 9 } 10 } 11 --------------------------------------------------- 12 public class Triangle { 13 double x; 14 double y; 15 double z; 16 public Triangle(){ 17 18 } 19 public Triangle(double x, double y, double z) { 20 this.x = x; 21 this.y = y; 22 this.z = z; 23 } 24 public double getX() { 25 return x; 26 } 27 public void setX(double x) { 28 this.x = x; 29 } 30 public double getY() { 31 return y; 32 } 33 public void setY(double y) { 34 this.y = y; 35 } 36 public double getZ() { 37 return z; 38 } 39 public void setZ(double z) { 40 this.z = z; 41 } 42 43 public double getArea(){ 44 double p=(x+y+z)/2; 45 double s=Math.sqrt(p*(p-x)*(p-y)*(p-z)); 46 return s; 47 } 48 public void showInfo(){ 49 System.out.println("三角形三边长为:"+x+" "+y+" "+z+" "); 50 double Area=getArea(); 51 System.out.println("三角形的面积为:"+Area); 52 } 53 54 } 55 ---------------------------------------------------------- 56 package Javashiyan9c; 57 /* 58 自定义一个三角形类Triangle.java,其中: 59 1)成员x,y,z作为三角形的三边长; 60 2)构造方法通过参数分别对x,y,z赋值; 61 3)两个方法getArea求面积,显示三角形信息showInfo。 62 这两个方法中当三条边不能构成三角形要抛出自定义异常NotTriangleException,否则显示正确信息。 63 另一个类TestTriangle,主方法中构造一个三角形对象Tri,三条边从控制台输入,显示三角形的信息和面积,要求捕获异常。 64 65 */ 66 import java.util.Scanner; 67 68 public class TriangleTest { 69 public static void main(String[] args) { 70 Scanner input=new Scanner(System.in); 71 Triangle Tri=new Triangle(); 72 System.out.println("请输入三角形三边:"); 73 double x=input.nextDouble(); 74 double y=input.nextDouble(); 75 double z=input.nextDouble(); 76 if(x+y>z&&x+z>y&&y+z>x){ 77 Tri.setX(x);Tri.setY(y);Tri.setZ(z); 78 }else{ 79 throw new NotTriangleException("输入的三边无法构成三角形"); 80 } 81 Tri.showInfo(); 82 Tri.getArea(); 83 84 } 85 }
1 package Javashiyan9b; 2 import java.util.Scanner; 3 /* 4 编写一个程序IntegerExcep.java。从命令行输入一个字符串, 5 将该字符串转换为double型数据,当数据不在【0,100】之间时抛出一个异常, 6 采用适当的机制来捕获并处理异常。可以用现成的异常也可以自定义异常。 7 */ 8 public class IntegerExcep { 9 public static void main(String[] args) { 10 Scanner input=new Scanner(System.in); 11 System.out.println("请输入:"); 12 String str= input.next(); 13 double st=Double.parseDouble(str); 14 try{ 15 if(st<0||st>100){ 16 throw new OutLimitsException(); 17 } 18 System.out.println(st); 19 }catch (OutLimitsException e){ 20 System.out.println("数据不在【0,100】之间"); 21 } 22 } 23 } 24 ---------------------------------------------------------------- 25 package Javashiyan9b; 26 27 public class OutLimitsException extends RuntimeException{ 28 public OutLimitsException() { 29 } 30 31 public OutLimitsException(String message) { 32 super(message); 33 } 34 }
1 package Javashiyan9c; 2 /* 3 自定义一个三角形类Triangle.java,其中: 4 1)成员x,y,z作为三角形的三边长; 5 2)构造方法通过参数分别对x,y,z赋值; 6 3)两个方法getArea求面积,显示三角形信息showInfo。 7 这两个方法中当三条边不能构成三角形要抛出自定义异常NotTriangleException,否则显示正确信息。 8 另一个类TestTriangle,主方法中构造一个三角形对象Tri,三条边从控制台输入,显示三角形的信息和面积,要求捕获异常。 9 10 */ 11 import java.util.Scanner; 12 13 public class TriangleTest { 14 public static void main(String[] args) { 15 Scanner input=new Scanner(System.in); 16 Triangle Tri=new Triangle(); 17 System.out.println("请输入三角形三边:"); 18 double x=input.nextDouble(); 19 double y=input.nextDouble(); 20 double z=input.nextDouble(); 21 if(x+y>z&&x+z>y&&y+z>x){ 22 Tri.setX(x);Tri.setY(y);Tri.setZ(z); 23 }else{ 24 throw new NotTriangleException("输入的三边无法构成三角形"); 25 } 26 Tri.showInfo(); 27 Tri.getArea(); 28 29 } 30 } 31 ------------------------------------------------------------ 32 package Javashiyan9c; 33 34 public class NotTriangleException extends RuntimeException{ 35 public NotTriangleException() { 36 } 37 38 public NotTriangleException(String message) { 39 super(message); 40 } 41 } 42 ------------------------------------------------------------ 43 package Javashiyan9c; 44 45 public class Triangle { 46 double x; 47 double y; 48 double z; 49 public Triangle(){ 50 51 } 52 public Triangle(double x, double y, double z) { 53 this.x = x; 54 this.y = y; 55 this.z = z; 56 } 57 public double getX() { 58 return x; 59 } 60 public void setX(double x) { 61 this.x = x; 62 } 63 public double getY() { 64 return y; 65 } 66 public void setY(double y) { 67 this.y = y; 68 } 69 public double getZ() { 70 return z; 71 } 72 public void setZ(double z) { 73 this.z = z; 74 } 75 76 public double getArea(){ 77 double p=(x+y+z)/2; 78 double s=Math.sqrt(p*(p-x)*(p-y)*(p-z)); 79 return s; 80 } 81 public void showInfo(){ 82 System.out.println("三角形三边长为:"+x+" "+y+" "+z+" "); 83 double Area=getArea(); 84 System.out.println("三角形的面积为:"+Area); 85 } 86 87 88 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通