我可以把这个switch语句再小一点吗?最好使用for循环

sbtkgmzw  于 2021-08-25  发布在  Java
关注(0)|答案(4)|浏览(360)

**已关闭。**此问题不符合堆栈溢出准则。它目前不接受答案。
**想要改进此问题?**更新问题,使其位于堆栈溢出主题上。

三天前关门。
改进这个问题
有没有办法使这个更小,我尝试使用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;
}
mrfwxfqh

mrfwxfqh1#

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

xkrw2x1b

xkrw2x1b2#

为此:

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();

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

1aaf6o9v

1aaf6o9v3#

以下是避免虚假案例陈述的最佳实践。首先,应该创建一些预定义关系的Map。然后,您可以在运行时从中获取值。

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();
}
tzcvj98z

tzcvj98z4#

迭代 .values() 这不是最优的。最好使用预先填充的哈希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();

相关问题