JAVA:语法错误插入else语句来完成if语句

时间:2021-02-16 22:23:08

Hi im making a plugin for my minecraft server but now i have a problem, im stuck with and if else statement while updating it. I have searched on a lot of forums but it didn't help so now im here. I use eclipse.

嗨即时为我的我的Minecraft服务器制作一个插件,但现在我有一个问题,我坚持和if else语句更新它。我在很多论坛上搜索过,但是现在我在这里没有帮助。我用eclipse。

        if(commandName.equals("t")){

        }
            if(!player.hasPermission("Clans.teamchat")) {
                player.sendMessage(ChatColor.RED + "You do not have permission to use this command.");
                return true;
            }
            if(!tPlayer.hasTeam()) {
                player.sendMessage(ChatColor.RED + "You are not on a team.");
                return true;
            }
            else if(!getRank(PlayerName).canTeamChat()) {
                player.sendMessage(ChatColor.RED + "You lack sufficient permissions to talk in team chat.");
                return true;
            }
            else if (args.length < 1) {
                player.sendMessage(ChatColor.RED + "You did not enter a message to send.");
                return true;
            }
            else if (args[0].equalsIgnoreCase("@loc")) {
            return true;
            }

2 个解决方案

#1


1  

Not sure it that's all the relevant code but could be that you are missing a final return statement after those if. The reason is that with your code as is now, you are not handling the case of not matching any of your conditions but you still need to provide a return value for your method.

不确定这是所有相关的代码,但可能是你错过了那些if之后的最终return语句。原因是,使用现在的代码,您不会处理不匹配任何条件但仍需要为方法提供返回值的情况。

#2


0  

You need to be more diligent here; it starts with formatting and braces. You really want that each block after an if, even stuff like

你需要在这里更勤奋;它从格式和大括号开始。你真的希望每个块后面的if,甚至是类似的东西

if (something) {
   just one thing 
} else {
   just another thing
}

comes as block within { and }. And then you format your code in a disciplined way (you know, any decent IDE can do that for you) - in order to simply be able to grasp the "content" that you are creating.

来自{和}中的块。然后你以一种规范的方式格式化你的代码(你知道,任何体面的IDE都可以为你做到这一点) - 为了能够掌握你正在创建的“内容”。

In other words: your problem is simply that you are violating syntax rules. And the easiest way to avoid that - is by writing source code that carries a structure that guides you.

换句话说:您的问题只是违反了语法规则。避免这种情况的最简单方法是编写带有引导您结构的源代码。

You see, probably you are missing a final return statement, or you put two elses after each other - all of that would become obvious, if you would just write down your code using the same rules and formatting constantly.

你看,可能你错过了一个最后的return语句,或者你把两个elses放在一起 - 如果你只是使用相同的规则和格式不断地写下你的代码,所有这些都会变得很明显。

The other thing of course: such lengthy if/else chains are always something that you want to avoid by doing proper OO designs. You don't ask for object properties to then make decisions; instead you use interfaces, that come with different implementations; so, later on, you just call those methods ( see here for a detailed explanation of that ).

另一件事当然是:这样冗长的if / else链总是你想要通过适当的OO设计来避免的东西。你不要求对象属性然后做出决定;而是使用具有不同实现的接口;所以,稍后,您只需调用这些方法(请参阅此处以获取详细说明)。

#1


1  

Not sure it that's all the relevant code but could be that you are missing a final return statement after those if. The reason is that with your code as is now, you are not handling the case of not matching any of your conditions but you still need to provide a return value for your method.

不确定这是所有相关的代码,但可能是你错过了那些if之后的最终return语句。原因是,使用现在的代码,您不会处理不匹配任何条件但仍需要为方法提供返回值的情况。

#2


0  

You need to be more diligent here; it starts with formatting and braces. You really want that each block after an if, even stuff like

你需要在这里更勤奋;它从格式和大括号开始。你真的希望每个块后面的if,甚至是类似的东西

if (something) {
   just one thing 
} else {
   just another thing
}

comes as block within { and }. And then you format your code in a disciplined way (you know, any decent IDE can do that for you) - in order to simply be able to grasp the "content" that you are creating.

来自{和}中的块。然后你以一种规范的方式格式化你的代码(你知道,任何体面的IDE都可以为你做到这一点) - 为了能够掌握你正在创建的“内容”。

In other words: your problem is simply that you are violating syntax rules. And the easiest way to avoid that - is by writing source code that carries a structure that guides you.

换句话说:您的问题只是违反了语法规则。避免这种情况的最简单方法是编写带有引导您结构的源代码。

You see, probably you are missing a final return statement, or you put two elses after each other - all of that would become obvious, if you would just write down your code using the same rules and formatting constantly.

你看,可能你错过了一个最后的return语句,或者你把两个elses放在一起 - 如果你只是使用相同的规则和格式不断地写下你的代码,所有这些都会变得很明显。

The other thing of course: such lengthy if/else chains are always something that you want to avoid by doing proper OO designs. You don't ask for object properties to then make decisions; instead you use interfaces, that come with different implementations; so, later on, you just call those methods ( see here for a detailed explanation of that ).

另一件事当然是:这样冗长的if / else链总是你想要通过适当的OO设计来避免的东西。你不要求对象属性然后做出决定;而是使用具有不同实现的接口;所以,稍后,您只需调用这些方法(请参阅此处以获取详细说明)。