创建枚举后,我想分配一个随机值并将其作为参数发送给函数,但是我遇到了麻烦。
public enum TGate
{
A, B, C, D
}
public class Parking
{
TGate gate;
gate=TGate.C;
public Parking(TGate gate)
{
switch gate.....
}
}
我希望有人可以帮助我。 感谢
答案 0 :(得分:0)
TGate gate;
gate = TGate.values()[(int)(Math.random()*TGate.values().length)];
并将此门传递给它。
答案 1 :(得分:0)
你可以这样试试:
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;
}
}