代码如下:
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int i = scanner.nextInt();
switch (i) {
case 1 :
System.out.println("输出:A");
break;
case 2 :
System.out.println("输出:B");
break;
case 3 :
System.out.println("输出:C");
break;
case 4 :
System.out.println("输出:D");
break;
case 5 :
System.out.println("输出:E");
break;
default :
System.out.println("输入数据有误!");
break;
}
}
}
测试效果:
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int num = 0;
System.out.print("请输入一个数字");
try {
num = scanner.nextInt();
} catch (Exception e) {
}
switch (num) {
case 1:
System.out.print("A");
break;
case 2:
System.out.print("B");
break;
case 3:
System.out.print("C");
break;
case 4:
System.out.print("D");
break;
case 5:
System.out.print("E");
break;
default:
System.out.print("输入数据有误");
break;
}
}
输入非数字异常处理