package com.day11; import java.util.*; public class Demo1 { public static void main(String[] args) { System.out.print("请输入课程代号(1~3之间的数字):"); Scanner in = new Scanner(System.in); try { int courseCode = in.nextInt(); switch (courseCode) { case 1: System.out.println("111编程"); break; case 2: System.out.println("222编程"); break; case 3: System.out.println("333编程"); } } catch (Exception ex) { System.out.println("输入不为数字!"); ex.printStackTrace(); } finally { System.out.println("欢迎提出建议!"); } } }
2.
package com.day11; public class People { private int age = 0; public void setAge(int age) throws Exception { if (age>=1 && age<=100) this.age = age; else { throw new Exception("年龄必须在1到100之间!"); } } }
package com.day11; public class Test { public static void main(String[] args) { People people = new People(); try { people.setAge(110); } catch (Exception e) { e.printStackTrace(); } } }