Java Integer在更改后更改第二个

时间:2014-07-25 20:45:14

标签: java

我的代码,

int selection = 0;
            if(checkKey(Keyboard.KEY_Z)){
                selection++;
                System.out.println(selection);
            }
            if(checkKey(Keyboard.KEY_B)){
                selection--;
                System.out.println(selection);
            }

似乎无法正常运作。在通过键功能更改后,整数选择似乎几乎立即变回0。

围绕它的代码是相关的

int selection = 0;
            if(checkKey(Keyboard.KEY_Z)){
                selection++;
                System.out.println(selection);
            }
            else if(checkKey(Keyboard.KEY_B)){
                selection--;
                System.out.println(selection);
            }
            if(selection < 14){
                selection = 1;
            }
            if(selection == -1){
                selection = 14;
            }
            if(Toggles.clickMenu){
                if(selection == 1){
                    var8.drawStringWithShadow(Strings.textsymbol + "b<", 62, 34, 0000);
                    if(this.mc.gameSettings.Enter.isPressed()){
                        Toggles.fly = !Toggles.fly;
                    }
                }else if(selection == 2.0){
                    var8.drawStringWithShadow(Strings.textsymbol + "b<", 62, 46, 0000);
                    if(this.mc.gameSettings.Enter.isPressed()){
                        Toggles.sprint = !Toggles.sprint;
                    }
                }else if(selection == 3){
                    var8.drawStringWithShadow(Strings.textsymbol + "b<", 62, 58, 0000);
                    if(this.mc.gameSettings.Enter.isPressed()){
                        Toggles.autojump = !Toggles.autojump;
                    }
                }
            }

这是更进一步的代码:

if(Toggles.clickMenu2){
                Gui.drawRect(85, 200, 161, 0, 0xaa000000);
                var8.drawStringWithShadow("Menu2", 90, 4, 0x00ff00);
                if(Toggles.off == true){
                    var8.drawStringWithShadow(Strings.textsymbol + "bSpeedMeUp", 87, 16, 0000);
                }else{
                    var8.drawStringWithShadow(Strings.textsymbol + "bSpeedMeUp", 87, 16, 0000);
                }
//              if(Toggles.off == true){
//                  var8.drawStringWithShadow(Strings.textsymbol + "b[=]", 144, 16, 0000);
//              }else{
//                  var8.drawStringWithShadow(Strings.textsymbol + "b[=]", 144, 16, 0000);
//              }
                if(Toggles.mute == true){
                    var8.drawStringWithShadow(Strings.textsymbol + "bGoFancy", 87, 28, 0000);
                }else{
                    var8.drawStringWithShadow(Strings.textsymbol + "bGoFancy", 87, 28, 0000);
                }
//              if(Toggles.mute == true){
//                  var8.drawStringWithShadow(Strings.textsymbol + "b[-]", 144, 28, 0000);
//              }else{
//                  var8.drawStringWithShadow(Strings.textsymbol + "b[-]", 144, 28, 0000);
//              }
                if(Toggles.noweather){
                    var8.drawStringWithShadow(Strings.textsymbol + "aNoWeather", 87, 40, 0000);
                }else{
                    var8.drawStringWithShadow(Strings.textsymbol + "cNoWeather", 87, 40, 0000);
                }
//              if(Toggles.noweather){
//                  var8.drawStringWithShadow(Strings.textsymbol + "a[F9]", 144, 40, 0000);
//              }else{
//                  var8.drawStringWithShadow(Strings.textsymbol + "c[F9]", 144, 40, 0000);
//              }
                if(Toggles.spider){
                    var8.drawStringWithShadow(Strings.textsymbol + "aSpider", 87, 52, 0000);
                    //var8.drawStringWithShadow(Strings.textsymbol + "a[F8]", 144, 52, 0000);
                }else{
                    var8.drawStringWithShadow(Strings.textsymbol + "cSpider", 87, 52, 0000);
                    //var8.drawStringWithShadow(Strings.textsymbol + "c[F8]", 144, 52, 0000);
                }
                if(Toggles.step){
                    var8.drawStringWithShadow(Strings.textsymbol + "aStep", 87, 64, 0000);
                    //var8.drawStringWithShadow(Strings.textsymbol + "a[F7]", 144, 64, 0000);
                }else{
                    var8.drawStringWithShadow(Strings.textsymbol + "cStep", 87, 64, 0000);
                    //var8.drawStringWithShadow(Strings.textsymbol + "c[F7]", 144, 64, 0000);
                }
            }else{
                    Gui.drawRect(85, 16, 161, 0, 0xaa000000);
                    var8.drawStringWithShadow(Strings.textsymbol + "cMenu2", 90, 4, 0x00ff00);

            }
            //THE UI! **** YES! IFNDAISGOFJOOHASF
            if(checkKey(Keyboard.KEY_Z)){
                selection++;
                System.out.println(selection);
            }
            else if(checkKey(Keyboard.KEY_B)){
                selection--;
                System.out.println(selection);
            }
            if(selection < 14){
                selection = 1;
            }
            if(selection == -1){
                selection = 14;
            }
            if(Toggles.clickMenu){
                if(selection == 1){
                    var8.drawStringWithShadow(Strings.textsymbol + "b<", 62, 34, 0000);
                    if(this.mc.gameSettings.Enter.isPressed()){
                        Toggles.fly = !Toggles.fly;
                    }
                }else if(selection == 2.0){
                    var8.drawStringWithShadow(Strings.textsymbol + "b<", 62, 46, 0000);
                    if(this.mc.gameSettings.Enter.isPressed()){
                        Toggles.sprint = !Toggles.sprint;
                    }
                }else if(selection == 3){
                    var8.drawStringWithShadow(Strings.textsymbol + "b<", 62, 58, 0000);
                    if(this.mc.gameSettings.Enter.isPressed()){
                        Toggles.autojump = !Toggles.autojump;
                    }
                }
            }
            //KeyChecker
            String actionsound = "random.levelup";
            String symbol = "§";
            if(this.mc.gameSettings.eClientMenu1.isPressed()){
                Toggles.clickMenu = !Toggles.clickMenu;
            }
            if(this.mc.gameSettings.eClientMenu2.isPressed()){
                Toggles.clickMenu2 = !Toggles.clickMenu2;
            }

            if(this.mc.gameSettings.eClientFly.isPressed()){
                Toggles.fly = !Toggles.fly;
                if(Toggles.fly == true){
                    mc.thePlayer.addChatMessage(new ChatComponentText(Strings.textsymbol + "aFly is now enabled")); 
                }else{
                    mc.thePlayer.addChatMessage(new ChatComponentText(Strings.textsymbol + "cFly is now disabled")); 
                }

                mc.theWorld.playSound(mc.thePlayer.posX+0.5D, mc.thePlayer.posY+0.5D, mc.thePlayer.posZ+0.5D, actionsound, 10.3F, 10.6F, false);
            }

CheckKey代码:

public GuiIngame(Session p_i1103_1_, Minecraft p_i1036_1_) {
        this.session = p_i1103_1_;
        String username = p_i1103_1_.getUsername();
        keyStates = new boolean [256];
        this.mc = p_i1036_1_;
        this.persistantChatGUI = new GuiNewChat(p_i1036_1_);
        this.field_152127_m = new GuiStreamIndicator(this.mc);
    }
    public boolean checkKey(int i){
        if(mc.currentScreen != null){
            return false;
        }

        if(Keyboard.isKeyDown(i) != keyStates[i]){
            return keyStates[i] = !keyStates[i];
        }else{
            return false;
        }
    }

1 个答案:

答案 0 :(得分:2)

嗯,两个条件都必须返回true。第一个条件的块递增选择变量,第二个条件的块递减它。

如果您只希望输入一个条件(最多),则应使用if:

        int selection = 0;
        if(checkKey(Keyboard.KEY_Z)){
            selection++;
            System.out.println(selection);
        }
        else if(checkKey(Keyboard.KEY_B)){
            selection--;
            System.out.println(selection);
        }