【Java面试题】定义枚举类型Weekday,使用枚举类型配合switch语句,尝试如下功能:

定义枚举类型Weekday,使用枚举类型配合switch语句,尝试如下功能:

wd = Mon是输出“Do Monday work”,wd = Tue时,输出“Do Monday work”,以此类推,当wd不为枚举值元素值时输出“I don`t know which is day”。

实现代码如下:

package workspace;

enum Weekday{Mon,Tue,Wed,Thu,Fri,Sat,Sun};

public class myday {
	public static void main(String[] args) {

		Weekday c1 = Weekday.Mon;
		switch (c1) {
		case Mon: {
			System.out.println("Do Monday work");
			break;
		}
		case Tue: {

			System.out.println("Do Tuesday work");
			break;
		}
		case Wed: {

			System.out.println("Do Wednessday work");
			break;
		}
		case Thu:{
			System.out.println("Do Thursday work");
		}
		case Fri:{
			System.out.println("Do Friday work");
		}
		case Sat:{
			System.out.println("Do Saturday work");
		}
		case Sun:{
			System.out.println("Do Sunday work");
		}
		default:{
			System.out.println("I don`t know which is day");
		}
		}
	}
}

输出:

Do Monday work

 

你可能感兴趣的:(趣味算法)