0

After creating an enum, I want to asign a random value and send it as an argument to a function, but I have trouble with it.

public enum TGate
{
  A, B, C, D
}

public class Parking
{
  TGate gate;
  gate=TGate.C;

  public Parking(TGate gate) 
  {
    switch gate.....
  }
}

I hope someone can help me. Thanks

Eamonn McEvoy
  • 8,349
  • 13
  • 49
  • 78
  • 1
    You can add the ability as part of the Enum object - have a look at http://stackoverflow.com/questions/8114174/how-to-randomize-enum-elements – mtripp100 Mar 28 '14 at 10:02
  • I cannot see you, what is the problem? –  Mar 28 '14 at 10:02

2 Answers2

0

you can write like :

 TGate gate;
 gate = TGate.values()[(int)(Math.random()*TGate.values().length)];

and pass this gate to funciton.

Shekhar Khairnar
  • 2,453
  • 3
  • 22
  • 43
0

You may try it this way:

public class Example {

    public static void main(String[] args) {
        new Parking(TGate.values()[(int) (Math.random() * TGate.values().length)]);
    }
}

enum TGate {
  A, B, C, D
}

class Parking {

    private final TGate gate;

    public Parking(TGate gate) {
        this.gate = gate;
    }
}
Harmlezz
  • 7,524
  • 23
  • 34