【问题标题】:Returning two values from an enum list从枚举列表中返回两个值
【发布时间】:2021-02-19 02:44:56
【问题描述】:

我有一个使用枚举命令制作的列表。我正在为 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”?我也是新手,所以请准确

【问题讨论】:

  • 返回EnumSet?
  • 迭代并打印返回值。或 System.out.println(java.util.Arrays.asList(yourenum.values()));
  • 您可能想放弃该程序并重新开始。想想你想要实现什么,而不是试图让你现有的代码工作。请遵守 Java 命名约定,以便其他人更容易理解您的代码。
  • 您的输入是REDtesta
  • 您提供的代码(您声称可以工作的部分 - 1 种颜色 -> 1 种测试)存在编译错误。

标签: java arrays enums


【解决方案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]

【讨论】:

  • 提示:在最近的Java中List.of
猜你喜欢
  • 2019-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
  • 1970-01-01
相关资源
最近更新 更多