有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从枚举列表返回两个值的java

我使用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”和“testb”,而是一个数字列表,似乎是内存地址之类的

我如何调整第一个代码段,使之成为红色输入时,返回的是“testa”和“testb”?我也是新来的,所以请准确一点


共 (1) 个答案

  1. # 1 楼答案

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

    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]