【问题标题】:Java could not resolve to a variableJava 无法解析为变量
【发布时间】:2014-08-07 15:57:42
【问题描述】:
if(text.contains(authcode) && text.contains("balance")){
    String balUser = text.split("]")[0];
    event.getSession().send(new ClientChatPacket("/money"));
}
if(text.contains("Balance: $")){
    text = text.split(": ")[1];
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    event.getSession().send(new ClientChatPacket("/m " + balUser + text));
}

不幸的是 balUser(在第二个 IF 语句中)在 Eclipse 中突出显示为“无法解析为变量”。我只是想知道我是否在某处做了一些不正确的语法。

【问题讨论】:

  • 您只是在第一个 if 声明中声明 balUser - 它不在第二个声明的范围内。
  • 请在发布问题时格式化您的代码 - 从阅读您的问题并可能想要回答问题的人的角度思考问题。您的帖子是他们希望看到的格式吗?如果没有,请修复它。 (在这种情况下,它绝对不是......)

标签: java scope


【解决方案1】:

是的。 balUser 定义在if 的范围内。只需在if 语句之外定义它:

String balUser = null;
if(text.contains(authcode) && text.contains("balance")) {
    balUser = ....
}

【讨论】:

  • 谢谢,正是我想要的。九分钟后回来选择你的答案!
  • 唯一的问题是,这段文本在另一个运行多次的 if 语句中,所以每次它都会将我的变量重置为 null(这很糟糕)。有没有办法在 if 语句之外设置变量并在内部访问它?
  • 是的。以同样的方式,只需在顶级 if 语句之外定义 String balUser = null;
【解决方案2】:

您在第一个 if 语句中声明您的 balUser String,因此它是本地范围的。

在第一个 if 语句之外声明它并在第二个语句中检查 nulls 以​​消除这种情况。

【讨论】:

    【解决方案3】:

    您的变量 String balUser 声明在一对大括号内,因此它的范围仅限于该代码块。

    如果你想让它在其他地方被知道,你需要在两个块都可以看到的地方声明它。

    在你的情况下:

    String balUser = null; // variable declared here
    if(text.contains(authcode) && text.contains("balance")){
        balUser = text.split("]")[0]; // remove declaration, just assignation
        event.getSession().send(new ClientChatPacket("/money"));
    }
        if(text.contains("Balance: $")){
            text = text.split(": ")[1];
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        event.getSession().send(new ClientChatPacket("/m " + balUser + text));
    }
    

    【讨论】:

      【解决方案4】:

      您在第一个 if 语句中声明了变量 balUser,因此它的范围是第一个 if 语句。如果你想在其他地方使用它,你应该在 if 语句之外声明它。

      String balUser = null;
      if(text.contains(authcode) && text.contains("balance")){
          balUser = text.split("]")[0];
          event.getSession().send(new ClientChatPacket("/money"));
      }
      if(text.contains("Balance: $")){
          text = text.split(": ")[1];
          try {
              Thread.sleep(2000);
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          event.getSession().send(new ClientChatPacket("/m " + balUser + text));
      }
      

      【讨论】:

        【解决方案5】:

        检查你的代码结构:

        if() {
            String balUser = ...;
        } else {
            event.getSession().send( ...   balUser);
        }
        

        我希望现在很明显变量是在第一个块中声明的,并在第二个块中引用它不存在。通常,java 和所有类 c 语言中所有标识符的范围都受到一对环绕的 {} 的限制。但是内部作用域可以看到外部作用域的标识符,所以这段代码是正确的:

        int i = 5;
        {
           int j = i;
        }
        

        【讨论】:

          猜你喜欢
          • 2011-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多