Extract the value of an enum class

2

I try to get the value of an enum class

When ordering a whole number by console, the code would be something like this:

//metodo class
public enum TipoDeMadera {

    ROBLE,CAOBA;
}

//metodo main

System.out.println("introduzca un numero para elegir madera, 1 para roble, 2 para caoba");
int numero = Scanner entrada.nextInt();

How could I show the chosen wood in console?

    
asked by davide 11j 28.11.2017 в 05:40
source

1 answer

2

The enum contain the values() method which returns an array with enum , in this case {ROBLE, CAOBA} where OAK is in the position zero (0) and MAOBA in the position one (1). For his exercise the exit would be

System.out.println(TipoDeMadera.values()[numero - 1]);
    
answered by 28.11.2017 / 06:21
source