1. 程式人生 > >Java sdut acm 1959 簡單列舉型別——植物與顏色

Java sdut acm 1959 簡單列舉型別——植物與顏色

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

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

 請定義具有red, orange, yellow, green, blue, violet六種顏色的列舉型別color,根據輸入的顏色名稱,輸出以下六種植物花朵的顏色: Rose(red), Poppies(orange), Sunflower(yellow), Grass(green), Bluebells(blue), Violets(violet)。如果輸入的顏色名稱不在列舉型別color中,例如輸入purple,請輸出I don't know about the color purple.

Input

 輸入資料有多行,每行有一個字串代表顏色名稱,顏色名稱最多30個字元,直到檔案結束為止。

Output

 輸出對應顏色的植物名稱,例如:Bluebells are blue. 如果輸入的顏色名稱不在列舉型別color中,例如purple, 請輸出I don't know about the color purple.

Example Input

blue
yellow
purple

Example Output

Bluebells are blue.
Sunflower are yellow.
I don't know about the color purple.

Hint

 請用列舉型別實現。

Author


程式碼實現:

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 input = new Scanner(System.in);
		while (input.hasNext()) {
			String inputColor = input.next();
			try{
				Color color = Color.valueOf(inputColor);
				System.out.println(color);
			}
			catch(Exception e){
				System.out.println("I don't know about the color "+inputColor+".");
			}
		}
	}
}