java switch用法小结

// 3.用枚举
	public enum sex {
		男, 女
	}

	public static void main(String[] args) throws IOException {
		AA a = new AA();
		a.aa(sex.女);
	}

	public void aa(sex a) {
		switch (a) {
			case 男:
				System.out.println("男");
				break;
			case 女:
				System.out.println("女");
				break;
			default:
				System.out.println("entry");
		}
	}
//2.用字符
public static void main(String[] args) throws IOException {
		char a;
		System.out.println("Enter a number from 1--3:");
		a = (char) System.in.read();
		switch (a) {
			case '1':
				System.out.println("输入的是--> 1");
				break;
			case '2':
				System.out.println("输入的是--> 2");
				break;
			default:
				System.out.println("entry");
		}
	}
//1.用做整型
public static void main(String[] args) throws IOException {
		char a;
		System.out.println("Enter a number from 1--3:");
		a = (char) System.in.read();
		switch (new Integer(a + "")) {// 不加""会变成AscII
			case 1:
				System.out.println("输入的是--> 1");
				break;
			case 2:
				System.out.println("输入的是--> 2");
				break;
			default:
				System.out.println("entry");
		}
	}

switch是不能用来判断字符串的!!

猜你喜欢

转载自java-worker-cr.iteye.com/blog/2001949