获取枚举中特定整数值的字符串值#

时间:2017-06-26 17:58:03

标签: c# enums

好的,这个问题可能非常简单而且很愚蠢,但我现在正在失利。

我有一个枚举声明如下:

public enum legende : int
{
    NONE = 0, 
    Création = 1, 
    Reprise = 2 , 
    Pause = 3, 
    Problème_instruction = 4,
    Fix_manquant = 5,
    Outil_non_adéquat = 6,
    Laser = 7,
    Pièces_manquantes = 8,
    Attente_qualité = 9,
    Dérogation = 10,
    Séquencement = 11,
    Charge_Planif_Insuff = 12,
    TERA = 13,
    Changement_d_OF = 14,
    OFF_Fin_shift = 1014,
    Inaccessible = 1015
};

我试图使用他的整数值来访问特定值(这是我无法完成它的地方):

public legende legend = new legende();
item.Action = Enum.Parse(legend, "2"); //random example of what I'm trying to do

2 个答案:

答案 0 :(得分:3)

您可以将整数强制转换为枚举类型:

//  leg will be legende.Reprise
legend leg = (legende)2;

如果我正确地理解你Action是一个字符串,那么你可以在枚举值上调用ToString();这将返回"Reprise"

item.Action = leg.ToString();

item.Action = ((legende)2).ToString();

或者当你最终做的时候:

item.Action = Convert.ToString((legende)2);

如果legende有两个或更多成员共享整数值2,那么这将为您提供第一个。{1}}。

"指数"不是正确的用语; "整数值"会更好。

答案 1 :(得分:2)

如果您真的想使用 Scanner sc = new Scanner(System.in); ArrayList<String[]> stats = new ArrayList<>(); //initialize a container to hold all the stats System.out.println("please enter match results:"); while(sc.hasNextLine()) { String input = sc.nextLine(); String[] results = input.split(" : "); if(results.length == 4) { stats.add(results); } else if(input.equals("stop")) break; else System.out.println("Error reading input"); }//end of while for(int i = 0; i < stats.size(); i++) { try{ System.out.println(stats.get(i)[0] + " vs " + stats.get(i)[1] + " : " + Integer.valueOf(stats.get(i)[2]) + " - " + Integer.valueOf(stats.get(i)[3])); }catch (Exception e) { //do nothing with any invalid input } }

please enter match results:
r : b : 5 : 4
r : c : 7 : 10
j : g : 3 : 9
stop
r vs b : 5 - 4
r vs c : 7 - 10
j vs g : 3 - 9
相关问题