简体   繁体   English

Java中如何有条件地执行许多类似的重复代码行?

[英]How to conditionally execute many similar repeating lines of code in Java?

I have no idea how to really phrase this question without showing my code, I'm sorry for the vague title.我不知道如何在不显示我的代码的情况下真正表达这个问题,对于模糊的标题,我很抱歉。 I have two blocks of code that I need to find a way to simplify.我有两个代码块,我需要找到一种简化的方法。 I don't know what to do, because to my knowledge, I can't just replace the numbers with an iterator and use a for loop.我不知道该怎么做,因为据我所知,我不能只用迭代器替换数字并使用 for 循环。 I want to repeat this to 100 cases with the same pattern.我想以相同的模式重复 100 个案例。 I very much don't want to type all this out, even copy-pasting sounds like a nightmare.我非常不想把所有这些都打出来,即使复制粘贴听起来也像是一场噩梦。 Is there any way at all to repeat this using loops?有没有办法使用循环重复这个?

Thank you:)谢谢:)

switch (count) {
    case 1: {
        binding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue1", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        break;
    }
    case 2: {
        binding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue1", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        binding2 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue2", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        break;
    }
    case 3: {
        binding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue1", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        binding2 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue2", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        binding3 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue3", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        break;
    }
}
switch (hotkeyConfig.bindingCount) {
    case 1:
        while (binding1.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue1);
        }
    case 2:
        while (binding1.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue1);
        }
        while (binding2.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue2);
        }
    case 3:
        while (binding1.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue1);
        }
        while (binding2.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue2);
        }
        while (binding3.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue3);
        }
}

Use an array, instead of three separate variables, for your binding1, binding2, binding3 .为您的binding1, binding2, binding3使用一个数组,而不是三个单独的变量。 It might look something like this - I'm kind of guessing at what the classes involved might be.它可能看起来像这样 - 我有点猜测所涉及的类可能是什么。

KeyBinding[] bindings = new KeyBinding[3];
for (int index = 0; index < count; index++) {
    bindings[index] = KeyBindingHelper.registerKeyBinding(
        new KeyBinding(
            "key.lightningtow.hotkey_keyValue" + (index + 1),
            InputUtil.Type.KEYSYM, 
            GLFW.GLFW_KEY_UNKNOWN, 
            "key.category.hotkey"));
}

and if you have your hot key config bind values in a similar array, your second snippet might look like this.如果你的热键配置绑定值在一个类似的数组中,你的第二个片段可能看起来像这样。

for (int index = 0; index < count; index++) {
    while (bindings[index].wasPressed()) {
        client.player.sendCommand(hotKeyConfigBindValue[index]);
    }
}

I'm assuming you didn't intend to omit the break statements from your second excerpt.我假设您不打算从第二个摘录中省略break语句。

For the first code block, just reverse the order of the individual case s and allow "fall through":对于第一个代码块,只需颠倒各个case的顺序并允许“通过”:

switch (count) {
    case 3: {
        binding3 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue3", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
    }
    case 2: {
        binding2 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue2", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
    }
    case 1: {
        binding1 = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.lightningtow.hotkey_keyValue1", InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey"));
        break;
    }
}

You could also reduce the above code by writing a createKeyBinding method:您还可以通过编写createKeyBinding方法来减少上述代码:

public KeyBinding createKeyBinding(int index) {
    return new KeyBinding("key.lightningtow.hotkey_keyValue" + index, InputUtil.Type.KEYSYM, GLFW.GLFW_KEY_UNKNOWN, "key.category.hotkey")
}

Then the first block of code becomes:那么第一块代码就变成了:

switch (count) {
    case 3: {
        binding3 = KeyBindingHelper.registerKeyBinding(createKeyBinding(3));
    }
    case 2: {
        binding2 = KeyBindingHelper.registerKeyBinding(createKeyBinding(2));
    }
    case 1: {
        binding1 = KeyBindingHelper.registerKeyBinding(createKeyBinding(1));
        break;
    }
}

Alternatively, you could use an array rather than individual variables.或者,您可以使用数组而不是单个变量。
Note that since I could not ascertain the type for the variables, such as binding3 , I use Object as the type.请注意,由于我无法确定变量的类型,例如binding3 ,因此我使用Object作为类型。

Object[] bindings = new Object[count];
for (int i = count; --i >= 0;) {
    bindings[i] = KeyBindingHelper.registerKeyBinding(createKeyBinding(i));
}

The same can be applied to the second code block – assuming that the order of the while statements is not important, ie这同样适用于第二个代码块——假设while语句的顺序不重要,即

switch (hotkeyConfig.bindingCount) {
    case 3:
        while (binding3.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue3);
        }
    case 2:
        while (binding2.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue2);
        }
    case 1:
        while (binding1.wasPressed()) {
            client.player.sendCommand(hotkeyConfig.bindValue1);
        }
}

However, if the order is important, then if you use an array (as shown above), the second code block can be written as:但是,如果顺序很重要,那么如果使用数组(如上所示),则第二个代码块可以写为:

for (int i = 0; i < hotkeyConfig.bindingCount; i++) {
    while (bindings[i].wasPressed()) {
        client.player.sendCommand(hotkeyConfig.bindValues[i]); // assumes appropriate array
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM