现在的位置: 首页 > 综合 > 正文

【流程控制】 – switch

2013年10月23日 ⁄ 综合 ⁄ 共 419字 ⁄ 字号 评论关闭

范例:

import java.util.Random;
public class Text {
	public static void main(String[] args) {
		Random ran = new Random();
		for(int i = 0;i<100;i++){
			int c = ran.nextInt(26) + 'a';
			System.out.print(c+"、"+(char)c+":");
			switch(c){
				case 'a':
				case 'e':
				case 'i':
				case 'o':
				case 'u':System.out.println("vowel");
						break;
				case 'y':
				case 'w':System.out.println("sometimes a vowel");
						break;
				default:System.out.println("consonant");
			}
		}
	}
}

可以看到,可以有多个case后面跟同一个操作,这样做简化代码。

操作执行后,一定不要忘记break。

抱歉!评论已关闭.