我可以让这个 switch 语句更小吗?最好有一个for循环

Can I make this switch statement any smaller? Preferably with a for loop

有没有办法让它更小,我尝试使用 for 循环但无法为每种可能的类型创建随机实例。

Random randFireworkEffect = new Random(5);
switch(randFireworkEffect.nextInt()) {
    case 0:
        e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BALL).trail(true).build();
        break;
    case 1:
        e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BALL_LARGE).trail(true).build();
        break;
    case 2:
         e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BURST).trail(true).build();
        break;
    case 3:
         e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.CREEPER).trail(true).build();
        break;
    case 4:
         e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.STAR).trail(true).build();
        break;
}

你可以使用.values()

Random randFireworkEffect = new Random();
e = FireworkEffect.builder()
        .flicker(true)
        .withColor(c)
        .withFade(c)
        .with(FireworkEffect.Type.values([randFireworkEffect.nextInt(5)])
        .trail(true)
        .build();

要做到这一点:

Random randFireworkEffect = new Random(5);
                                switch(randFireworkEffect.nextInt()) {
                                case 0:
                                    e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BALL).trail(true).build();
                                    break;
                                case 1:
                                    e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BALL_LARGE).trail(true).build();
                                    break;
                                case 2:
                                     e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.BURST).trail(true).build();
                                    break;
                                case 3:
                                     e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.CREEPER).trail(true).build();
                                    break;
                                case 4:
                                     e = FireworkEffect.builder().flicker(true).withColor(c).withFade(c).with(Type.STAR).trail(true).build();
                                    break;

更短,看看所有这些行之间到底有什么区别,它是类型:

Type[] types = new Type[]{Type.BALL, Type.BALL_LARGE, Type.BURST, Type.CREEPER, Type.STAR};
Random effect = new Random(5);
e = FireworkEffect.builder().flicker(true)
    .withColor(c).withFade(c)
    .with(types[effect.nextInt()])
    .trail(true).build();

是的,您可以将它放在一个循环中,以设置更多值。

有以下最佳做法可以避免虚拟案例陈述。首先应该创建一些预定义关系图。然后您就可以在运行时从中获取值。

private static final Map<Integer, Type> typeMap = new Hashmap<>();

static {
  typeMap.put(0, Type.BALL);
  typeMap.put(1, Type.BALL_LARGE);
  typeMap.put(2, Type.BURST);
  typeMap.put(3, Type.CREEPER);
  typeMap.put(4, Type.STAR);
}

public FireworkEffect getFireworkEffect() {
   Random randFireworkEffect = new Random(5);
   Type type = typeMap.get(randFireworkEffect);
   return FireworkEffect.builder()
          .flicker(true)
          .withColor(c)
          .withFade(c)
          .with(type)
          .trail(true)
          .build();
}

遍历 .values() 不是最优的。最好使用 pre-populated hash map

Map<Integer, Type> typeMap = new HashMap<>();
typeMap.put(0, Type.BALL);
typeMap.put(1, Type.BALL_LARGE);
// ... other options

Random randFireworkEffect = new Random(5);
e = FireworkEffect.builder()
        .flicker(true)
        .withColor(c)
        .withFade(c)
        .with(typeMap.get(randFireworkEffect.nextInt()))
        .trail(true)
        .build();