Java sdut acm 1959 簡單列舉型別——植物與顏色
阿新 • • 發佈:2019-02-09
簡單列舉型別——植物與顏色
Time Limit: 1000MS Memory Limit: 65536KBProblem 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
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+"."); } } } }