1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| package ADV_0; import java.io.FileInputStream; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class ADVZc0 { public static void main(String[] args) { show(); try { show1(); } catch (Exception e) { e.printStackTrace(); } System.out.println("==div程序开始执行...=="); try { System.out.println(div(10, 0)); System.out.println("底层方法执行成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("底层方法执行失败"); } System.out.println("==程序结束=="); System.out.println("==age0程序开始执行...=="); try { saveAge0(300); System.out.println("成功"); } catch (ItheimaAgeIllegalException e) { e.printStackTrace(); System.out.println("失败"); } System.out.println("==程序结束=="); System.out.println("==age1程序开始执行...=="); try { saveAge0(300); System.out.println("成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("失败"); } System.out.println("==程序结束=="); System.out.println("==程序开始执行show2...=="); try { show2(); System.out.println("操作成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("操作失败"); } System.out.println("==程序结束=="); System.out.println("==shop程序开始执行...=="); while (true) { try { double price = userInputPrice(); System.out.println("用户成功设置了商品定价:" + price); break; } catch (Exception e) { System.out.println("您输入的数据是瞎搞的,请不要瞎输入价格!"); } } System.out.println("==程序结束=="); } public static void show(){ System.out.println("==程序开始执行show...=="); int[] arr = {1,2,3}; System.out.println("==程序结束=="); } public static void show1() throws Exception { System.out.println("==程序开始执行show1...=="); String str = "2024-11-25 8:30:00"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = sdf.parse(str); System.out.println(date); InputStream is = new FileInputStream("D:/tupian.png"); System.out.println("==程序结束=="); } public static int div(int a, int b) throws Exception { if(b == 0){ System.out.println("除数不能为0,您的参数有问题!"); throw new Exception("除数不能为0,您的参数有问题!(红)"); } int result = a / b; return result; } public static void saveAge0(int age) throws ItheimaAgeIllegalException { if(age < 1 || age > 200){ throw new ItheimaAgeIllegalException("年龄非法 age 不能低于1岁不能高于200岁"); }else { System.out.println("年龄合法"); System.out.println("保存年龄:" + age); } } public static void saveAge1(int age) { if(age < 1 || age > 200){ throw new ItheimaAgeIllegalRuntimeException("年龄非法 age 不能低于1岁不能高于200岁"); }else { System.out.println("年龄合法"); System.out.println("保存年龄:" + age); } } public static void show2() throws Exception { String str = "2024-07-09 11:12:13"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = sdf.parse(str); System.out.println(date); InputStream is = new FileInputStream("D:/tupian.png"); } public static double userInputPrice(){ Scanner sc = new Scanner(System.in); System.out.println("请您输入商品定价:"); double price = sc.nextDouble(); return price; } }
class ItheimaAgeIllegalException extends Exception{ public ItheimaAgeIllegalException() { } public ItheimaAgeIllegalException(String message) { super(message); } }
class ItheimaAgeIllegalRuntimeException extends RuntimeException{ public ItheimaAgeIllegalRuntimeException() { } public ItheimaAgeIllegalRuntimeException(String message) { super(message); } }
|