1. 程式人生 > >5月8日java上機任務

5月8日java上機任務

res row class col hex 數據 private tid static

 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上機任務