-3

我正在尝试调试一段代码,但出现“重复的局部变量”错误。我将如何解决这个问题?我不确定错误是什么,所以我在这里问。

public JumpPlusPlayer(JumpPlus plugin, Player p) {
    loadPermissions(p, plugin);
    fillConfig(plugin);
  }

  protected void loadPermissions(Player p, JumpPlus plugin) {
      HashSet<PermissionAttachmentInfo> perms = new HashSet<PermissionAttachmentInfo>();
    PermissionAttachment attach;
    if (plugin.usingPEX) {
      PermissionUser user = PermissionsEx.getUser(p);
      String world = p.getWorld().getName();
      attach = new PermissionAttachment(plugin, p);
      for (String perm : user.getPermissions(world)) {
        String expression = user.getMatchingExpression(perm, world);
        perms.add(new PermissionAttachmentInfo(p, perm, attach, user.explainExpression(expression)));
      }
    } else {
      perms = (HashSet<PermissionAttachmentInfo>) p.getEffectivePermissions();
    }

    for (PermissionAttachmentInfo attach : perms) {
      String perm = attach.getPermission();
      if (perm.contains("jumpplus.config.")) {
        String[] aux = perm.split("jumpplus.config.");
        aux = aux[1].split("-");
        if (aux[0].equals("hspeed"))
          this.hSpeed = Double.valueOf(Double.parseDouble(aux[1]));
        else if (aux[0].equals("vspeed"))
          this.vSpeed = Double.valueOf(Double.parseDouble(aux[1]));
        else if (aux[0].equals("maxjumps"))
          this.maxJumps = Integer.valueOf(Integer.parseInt(aux[1]));
        else if (aux[0].equals("maxfreejumps"))
          this.maxFreeJumps = Integer.valueOf(Integer.parseInt(aux[1]));
        else if (aux[0].equals("jumpcost"))
          this.jumpCost = Integer.valueOf(Integer.parseInt(aux[1]));
        else if (aux[0].equals("fallmodifier"))
          this.fallModifier = Integer.valueOf(Integer.parseInt(aux[1]));
        else if (aux[0].equals("particleeffect"))
          this.particleEffect = Boolean.valueOf(Boolean.parseBoolean(aux[1]));
        else if (aux[0].equals("defaultstate"))
          this.enable = Boolean.valueOf(Boolean.parseBoolean(aux[1]));
      }
    }
  }
4

2 回答 2

2

我将如何解决这个问题?

嗯,不要在同一个作用域内两次声明一个局部变量?

在增强的 for 循环中使用不同的局部变量名称,或者将第一个变量的声明移到if语句中:

PermissionAttachment attach = new PermissionAttachment(plugin, p);

(你不在语句之外使用if,那为什么要在开始时声明它?)

于 2013-02-28T09:37:39.073 回答
0

问题出在您的loadPermissions方法上,尤其是这两行:

PermissionAttachment attach;
for (PermissionAttachmentInfo attach : perms) {

第一行声明了一个名为attach. 第二行声明了一个名为 的局部变量attach,但由于它已经存在,因此不能这样做。您需要为其中一个选择不同的名称。

于 2013-02-28T09:38:32.677 回答