How to pass variable by parameter created in Enum in Java?

Asked

Viewed 951 times

1

I created a Class enum of java

public enum Cor {
    AZUL(1),VERMELHO(2),VERDE(3);
    private int var;
     Cor(int var)
    {
    this.var = var;
    }
}

In the main I’ll create a menu using do/while and switch/case and will be shown the colors, how to pick the chosen color and pass in a method parameter?

On the menu it goes like this

                System.err.println("Escolha uma cor");
                System.err.println("1-Azul");
                System.err.println("2-Vermelho");
                System.err.println("3-Verde");

The user type 1 must catch the color Blue.

  • You want the value of var? Example: Cor minhaCor = Cor.AZUL; and then int valorCor = minhaCor.val, something like this you want? If yes, just create a getter within the Enum: public int getVar() { return this.var; } .... minhaCor.getVar()

  • @Alfredbaudisch take a look at the edition

2 answers

3

From what I understand you’d like to make a menu of the kind:

  1. BLUE
  2. RED
  3. GREEN

If that’s the case, you can do the following. To display the menu:

for(Cor cor : Cor.values()) { System.out.println(cor.getIndice() + ". " + cor.name()); }

To select the color, the user can enter an Indice. This way you can add the following method on Enum:

public static Cor fromIndice(int indice){
    for(Cor cor : Cor.values()) {
        System.out.println(cor.getIndice() + "-" + cor.name());
    }
    throw new IllegalArgumentException("Indice invalido");
}

That when receiving an input returns a color.

If not, add more information.

2


Let’s go there are two examples:

Switch To use the switch it is necessary to create Constants with their values:

public static final int AZUL= 1;
public static final int VERMELHO= 2;
public static final int VERDE= 3;

public static Cor selecaoPorSwitch(int selecao){
    Cor cor= null;
    switch (selecao) {
    case AZUL:
        cor = Cor.AZUL;
        break;
    case VERMELHO:
        cor = Cor.VERMELHO;
        break;
    case VERDE:
        cor = Cor.VERDE;
        break;
    }
     return cor;
}

if / Else

Simple comparison of the value with the selection. For this it is necessary to create the getVar() to gain access to the value of each enum:

public static Cor selecaoPorIf(int selecao){
    Cor cor = null;
    if(selecao == Cor.AZUL.getVar()){
        cor = Cor.AZUL;
    }else if(selecao == Cor.VERDE.getVar()){
        cor = Cor.VERDE;
    }else if(selecao == Cor.VERMELHO.getVar()){
        cor = Cor.VERMELHO;
    }
    return cor;
}

How to test:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Selecione uma cor: \n 1-Azul\n2-Vermelho\n3-verde \n :");
    int selecao = scan.nextInt();
    Cor cor = selecaoPorIf(selecao);
    System.out.println("selecaoPorIf : "+cor);

    cor = selecaoPorSwitch(selecao);
    System.out.println("selecaoPorSwitch : "+cor);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.