如何修复Java程序中的括号?

时间:2016-10-25 21:56:31

标签: java plugins minecraft bukkit

我的括号有问题,我做错了什么。我在某处试图修复错误时添加了一个括号,它要么说我需要elseStatment,要么我需要添加另一个括号,当我添加其他括号时,我得到一个很大的错误,说“无法访问的代码”。

如果有人能解释为什么会发生这种情况并帮助我解决问题,我将不胜感激。为了向您显示错误发生的位置,我将放置3个感叹号。

package me.bukkit.Ragnarok;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import net.minecraft.server.v1_10_R1.PlayerSelector;

public class StandardFlight extends JavaPlugin {

    @Override
    public void onEnable() {
        getLogger().info("StandardFlight: Flight plugin has been loaded successfully!");

    }

    @Override
    public void onDisable() { 

    } **!!!**

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        if (cmd.getName().equalsIgnoreCase("flight") && sender instanceof Player) {
            Player player = (Player) sender;
            if(player.hasPermission("uheal.heal.self")) {

            if (args.length == 0) {
            player.sendMessage(ChatColor.GOLD + "Ops, try " + ChatColor.RED + "/flight on " + ChatColor.GOLD + "or " + ChatColor.RED + "/flight off");
            return true;

            if (args.length == 1) {
            } else  if (args[0].equals("on")) {
                player.setAllowFlight(true);
                player.setFlying(true);
                player.sendMessage(ChatColor.GOLD + "Setting Flight for player: " + ChatColor.RED + player.getName() + ChatColor.GOLD + ChatColor.BOLD + " On!");
                return true;

                if (args.length == 1) {
            } else if (args[0].equals("off")) {
                    player.setAllowFlight(false);
                    player.setFlying(false);
                    player.sendMessage(ChatColor.GOLD + "Setting Flight for player: " + ChatColor.RED + player.getName() + ChatColor.GOLD + ChatColor.BOLD + " Off!");  
                    return true;

            }
            return false;

        }
        {

    }

    }

} **!!!**

2 个答案:

答案 0 :(得分:2)

此:

if (args.length == 0) {
     player.sendMessage(ChatColor.GOLD + "Ops, try " + ChatColor.RED + "/flight on " + ChatColor.GOLD + "or " + ChatColor.RED + "/flight off");
     return true;

     if (args.length == 1) {

请注意注意事项。如果args.length为0,则此代码块只能执行,这意味着args.length == 1可以 NEVER 为真。而且,由于您return无条件地在该秒之前,因此,您的"无法访问的代码"。

所以..你可能在那里错过}

if (args.length == 0) {
    player.sendMessage(ChatColor.GOLD + "Ops, try " + ChatColor.RED + "/flight on " + ChatColor.GOLD + "or " + ChatColor.RED + "/flight off");
    return true;
} else if (args.length == 1) {
^^^^^^^^^^---NOTE THIS

如果您花时间正确格式化代码,这应该是显而易见的。

答案 1 :(得分:0)

你有很多问题,但我想出的是有适当的括号,至少我认为它们是基于我认为你想要做的事情:

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import net.minecraft.server.v1_10_R1.PlayerSelector;

public class StandardFlight extends JavaPlugin {

    @Override
    public void onEnable() {
        getLogger().info("StandardFlight: Flight plugin has been loaded successfully!");

    }

    @Override
    public void onDisable() { 

    } **!!!**

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

        if (cmd.getName().equalsIgnoreCase("flight") && sender instanceof Player) {
            Player player = (Player) sender;
            if(player.hasPermission("uheal.heal.self")) {

            if (args.length == 0) {
                player.sendMessage(ChatColor.GOLD + "Ops, try " + ChatColor.RED + "/flight on " + ChatColor.GOLD + "or " + ChatColor.RED + "/flight off");
                return true;
            }
            if (args.length == 1) {

            } else  if (args[0].equals("on")) {
                player.setAllowFlight(true);
                player.setFlying(true);
                player.sendMessage(ChatColor.GOLD + "Setting Flight for player: " + ChatColor.RED + player.getName() + ChatColor.GOLD + ChatColor.BOLD + " On!");
                return true;
                //This code is unreachable because you have a return statement above it. You need to move the code below it. 
                if (args.length == 1) {

                } else if (args[0].equals("off")) {
                    player.setAllowFlight(false);
                    player.setFlying(false);
                    player.sendMessage(ChatColor.GOLD + "Setting Flight for player: " + ChatColor.RED + player.getName() + ChatColor.GOLD + ChatColor.BOLD + " Off!");  
                    return true;

            }
            return false;

        }
    }
}

}

} **!!!**