从枚举列表返回两个值



我有一个使用enum命令的列表。我正在为java做一些编码练习,这里我要求用户输入一种颜色。根据输入的颜色,它将输入相应的样例值(为了学习如何做到这一点,变量名已经更改),它将返回与该颜色相关的样例测试词。

在下面的第一个代码段中,我已经实现了这一点,如果您输入RED,那么您将得到的输出是testa。

import java.util.Scanner; 
import java.util.InputMismatchException;
import java.util.EnumSet;
public class PLTypeEnum
{
enum colours { RED, YELLOW, BLUE, GREEN }

enum example { testa, testb, testc, testd}

private static <E extends Enum<E>> E getEnumElement(String elementExample, Class<E> elementExampleName)
{
boolean haveResult = false;
E result = null;
Scanner stdin = new Scanner(System.in);

while ( ! haveResult )
{
System.out.print("Input " + elementExample + ": ");
try
{
result = Enum.valueOf(elementExampleName, stdin.next().toUpperCase());
haveResult = true;
}
catch (IllegalArgumentException e)
{
System.out.println("Not a valid " + elementExample + ".");
stdin.nextLine(); // skip the invalid input
}
}

return result;
}

private static colours PLType2pl(example plt)
{
colours name = null;

switch (plt)
{
case RED:
name = colours.testa;
break;
case YELLOW:
name = colours.testb;
break;
case BLUE:
name = colours.testc;
break;
case GREEN:
name = colours.testd;
break;
}

return name;
}
public static void main(String[] args)
{
System.out.print("Known colours = ");
for (example t : EnumSet.allOf(example.class)) 
{
System.out.print(t + " ");
}
System.out.println();

example plt = getEnumElement("colour", example.class);
System.out.println(plt + " is of : " + PLType2pl(plt));
}
}

但是我的问题是,这段代码都出错了,如果例如,我希望RED与例子中的两个值相关联,让我们说testa和testb,那么这段代码不起作用,我已经尝试了这么多不同的可能性,但我不能弄清楚,即使它应该很容易....例如,我想做这样的事情(但这会导致错误)

private static colours PLType2pl(example plt)
{
colours name = null;

switch (plt)
{
case RED:
name = colours.testa, colours.testb;
break;
case YELLOW:
name = colours.testb;
break;
case BLUE:
name = colours.testc;
break;
case GREEN:
name = colours.testd;
break;
}

return name;
}

我试过制作"name"一个数组,但是当我这样做的时候,我得到的值就不再是"testa"one_answers";testb"而是一个数字列表,看起来像是内存地址之类的。

我如何调整第一个代码段,使其在输入红色时,返回的是" test_& quot;和"testb" ?此外,我是新手,所以请准确

这个问题其实很容易解决:

public enum Color {
RED(Arrays.asList("testa","tastb")),
YELLOW(Arrays.asList("testc","tastd","teste"));
Color(List colours){
this.colours = colours;
}
private List<String> colours;
public List<String> getColours(){
return this.colours;
}
}

--------------test------------------
public class TestColor {
public static void main(String[] args) {
String input = "RED";
System.out.println(Color.valueOf(input).getColours().toString());
}
}

--------------- 输出 ----------------

[testa, tastb]