简体   繁体   English

如果为其他语句,则获得两倍的输出

[英]If Else Statement, got twice the Output

In my IRC Bot, as soon as someone types !highfivetest the bot gives out a message twice. 在我的IRC Bot中,只要有人输入!highfivetest,该漫游器就会发出两次消息。 Is something wrong with my if-else? if-else是否有问题?

Arry String; Arry String;

public String[] moderatoren = {"furby1987","furbybot"}; // Moderatoren

CmdOutput; CmdOutput;

        if (message.equalsIgnoreCase("!highfivetest")){
            for(int i2 = 0; i2 < moderatoren.length; i2++){  
                if (sender.equals(moderatoren[i2])){
                    sendMessage (channel, "!highfive");
                }else{              
                    sendMessage(channel, "Nur eingetragene Moderatoren haben Zugriff auf diesen Befehl. Sry <3"); 
                }
            }
        }

Usually, i just type it that way; 通常,我只是这样输入。

            if (message.equalsIgnoreCase("!highfivetest")){
                for(int i2 = 0; i2 < moderatoren.length; i2++){  
                    if (sender.equals(moderatoren[i2])){
                        sendMessage (channel, "!highfive");
                    break;
                    }else{              
                        sendMessage(channel, "Nur eingetragene Moderatoren haben Zugriff auf diesen Befehl. Sry <3"); 
                    break;
                    }
                }
            }

But are the breaks really needed? 但是真的需要休息吗? just a bit irritated right now :-) 现在有点恼火:-)

Greetings! 问候!

The error message should be sent outside the for loop. 错误消息应在for循环发送。 Otherwise it will be printed for every moderator that did not send the !highfivetest message. 否则,它将为每个未发送!highfivetest消息的主持人打印。 To simplify the code, you could change the array of moderators to a List and then use the List.contains method. 为了简化代码,可以将主持人的数组更改为List ,然后使用List.contains方法。

Example 1: 范例1:

boolean vonModeratorGesendet = false;
for (int i2 = 0; i2 < moderatoren.length; i2++) {
    if (sender.equals(moderatoren[i2])) {
        vonModeratorGesendet = true;
        // you could also send "!highfive" from here
        break;
    }
}
if (vonModeratorGesendet) {
    sendMessage (channel, "!highfive");
} else {
    sendMessage(channel, "Nur eingetragene Moderatoren haben Zugriff auf diesen Befehl. Sry <3");
}

Example 2: 范例2:

List<String> moderatorenListe = Arrays.asList(moderatoren);
if (moderatorenListe.contains(sender)) {
    sendMessage (channel, "!highfive");
} else {
    sendMessage(channel, "Nur eingetragene Moderatoren haben Zugriff auf diesen Befehl. Sry <3");
}

Example 2 is the better option. 示例2是更好的选择。 Instead of converting the array to a List , you could also use a List at all the time (that's what I would do). 除了将数组转换为List ,您还可以一直使用List (这就是我要做的)。

您的if-else很好,问题可能出在for循环中或其他地方。

replace the breaks with continues, it's a loop you're running. 用继续替换休息,这是您正在运行的循环。

            if (message.equalsIgnoreCase("!highfivetest")){
            for(int i2 = 0; i2 < moderatoren.length; i2++){  
                if (sender.equals(moderatoren[i2])){
                    sendMessage (channel, "!highfive");
                continue;
                }else{              
                    sendMessage(channel, "Nur eingetragene Moderatoren haben Zugriff auf diesen Befehl. Sry <3"); 
                continue;
                }
            }
        }

That should work, continue is the c++ syntax for it as I'm unfamiliar with java but if you hunt there will be a similar line in the java language if it isn't continue. 那应该行得通,因为我不熟悉java,所以继续使用它的c ++语法,但是如果您不熟悉的话,在Java语言中也会出现类似的行,如果不继续的话。

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

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