首页 > 编程语言 > 详细

5月8日java上机任务

时间:2019-05-08 15:29:07      阅读:159      评论:0      收藏:0      [点我收藏+]
 1 package work;
 2 
 3 import java.util.Scanner;
 4 
 5 public class ExceptionTest {
 6 
 7     public static void main(String[] args) {
 8         Scanner in = new Scanner(System.in);
 9         double n1 = in.nextDouble();
10         double n2 = in.nextDouble();
11         double result;
12         try{
13             result = n1/n2;    
14             System.out.println(result);
15         }catch(ArithmeticException e){
16             e.printStackTrace();
17         }finally{
18             System.out.println("finally");    
19         }
20         in.close();
21     }
22 
23 }
 1 package work;
 2 
 3 import java.util.InputMismatchException;
 4 import java.util.Scanner;
 5 
 6 public class Work2 {
 7     public static void main(String[] args) {
 8         Scanner in = new Scanner(System.in);
 9         double radius;
10         try{
11             radius = in.nextDouble();
12             System.out.println(radius);
13         }catch(InputMismatchException e)
14         {
15             System.out.println("您输入的数据有问题");
16         }
17     }
18     }
 1 package work;
 2 
 3 public class Person {
 4     private String name;
 5     private int age;
 6     private String id;
 7     public String getName() {
 8         return name;
 9     }
10     public void setName(String name) {
11         this.name = name;
12     }
13     public int getAge() {
14         return age;
15     }
16     public void setAge(int age) {
17         this.age = age;
18     }
19     public String getId() {
20         return id;
21     }
22     public void setId(String id) throws IllegalArgumentException{
23         if(id.length()!=18)
24         {
25             throw(new IllegalArgumentException());
26         }
27         this.id = id;
28     }
29 }
1 package work;
2 
3 public class IllegalArgumentException extends Exception{
4     
5 }
 1 package work;
 2 
 3 public class ExceptionTest2 {
 4 
 5     public static void main(String[] args) {
 6         Person p1 = new Person();
 7         Person p2 = new Person();
 8         try {
 9             p1.setId("430122200009190315");
10             p2.setId("110110110");
11         } catch (IllegalArgumentException e) {
12             System.out.println("您输入的身份证长度有误");
13         }
14 
15 
16     }
17 
18 }

 

5月8日java上机任务

原文:https://www.cnblogs.com/sucker/p/10831926.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!