Demo entry 6623840

ExceptionDemo

   

Submitted by anonymous on Jun 10, 2017 at 13:54
Language: Java. Code size: 766 Bytes.

package ExceptionPackage;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionDemo {
	public static void main(String[] args) {
		Scanner reader  = new Scanner(System.in);
		int ch;
		String str[] = {"篮球","足球","排球","乒乓球","羽毛球"};
		System.out.println("请问您喜欢哪类运动");
		for(int i=0;i<str.length;i++){
			System.out.println((i+1)+str[i]+"");
		}System.out.println("\n请选择");
		try{
			ch = reader.nextInt();
			System.out.println("您选择的是"+str[ch-1]);
		}catch(InputMismatchException e){
			System.out.println("发生异常:输入格式错误"+e.toString());
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println("发生异常:数组下标越界"+e.toString());
		}catch(Exception e){
			System.out.println("发生异常:未知异常"+e.toString());
		}
	}
} 

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).