1 package demo2; 2 3 import java.util.InputMismatchException; 4 import java.util.Scanner; 5 6 /** 7 * 根据编号输出课程名称,输入错误处理异常并提示 8 * @author 9 * 10 */ 11 public class Course { 12 public static void main(String[] args) { 13 Scanner input=new Scanner(System.in); 14 try { 15 System.out.print("请输入课程代号(1-3之间的数字):"); 16 int num1=input.nextInt(); 17 switch(num1) { 18 case 1: 19 System.out.println("JAVA课程"); 20 break; 21 case 2: 22 System.out.println("C#课程"); 23 break; 24 case 3: 25 System.out.println("WEB前端课程"); 26 break; 27 default: 28 System.out.println("输入数字错误!"); 29 break; 30 } 31 }catch(InputMismatchException e) { 32 System.out.println("输入的不是整数!"); 33 System.err.println(e.getMessage()); 34 }catch(Exception e) { 35 System.err.println("其他未知异常!"); 36 System.err.println(e.getMessage()); 37 } 38 System.out.println("欢迎提出建议!"); 39 } 40 }
原文地址:https://www.cnblogs.com/baichang/p/10066943.html
时间: 2024-12-29 09:47:39