1. 程式人生 > >簡單列舉型別——植物與顏色(類)

簡單列舉型別——植物與顏色(類)

import java.util.Scanner;
enum Color{
	red("Rose", "red"),
	orange("Poppies", "orange"),
	yellow("Sunflower", "yellow"),
	green("Grass", "green"),   
    blue("Bluebells", "blue"),   
    violet("Violets", "violet"); 
	
	String name;
	String color;
	Color(String name, String color) {
		this.name = name;
		this.color = color;
	}
	public String toString() {
		return name + " are " + color + "."; 
	}
}
public class Main {
	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		while(reader.hasNext()) {
			String incolor = reader.nextLine();
			try {
				Color color = Color.valueOf(incolor);
				System.out.println(color);
			}
			catch(Exception e) {
				System.out.println("I don't know about the color "+incolor+".");
			}
		}
	}

}