为什么我会收到“找不到符号"?编译时java程序出错?

2022-01-19 00:00:00 return java

我试图在我的代码末尾返回我的布尔变量 localFound 的值,但是当我编译时,我得到一个错误,说它找不到符号.我知道这是一个处理变量范围的错误,但我不知道如何修复它.如何让我的程序返回正确的值?谢谢.

I am trying to return the value of my boolean variable localFound at the end of my code but when I compile, I get an error that says it cannot find the symbol. I know this is an error that deals with the scope of the variable, but I do not know how to fix it. How do I get my program to return the correct value? Thanks.

public static boolean addIfNotEmpty(DvdTreeNode root, String movieToCommand) {

  if (root == null) {
    return false;
  }
  addIfNotEmpty(root.getRight(), movieToCommand);
  if (root.getItem().getTitle().equalsIgnoreCase(movieToCommand)) {
    root.getItem().addCopy();
    System.out.println("You have added another copy of ""
    + movieToCommand
    + "" to the inventory.");
    boolean localFound;
    localFound = true;
  }
  addIfNotEmpty(root.getLeft(), movieToCommand);
  return localFound;
} // end addIfNotEmpty 

推荐答案

localFound 未在您的 return 语句的范围内定义.它只存在于您的 if 语句中.

localFound is not defined in the scope of your return statement. It only exists within your if statement.

将声明移到 if 语句之外,并将其初始化为某个默认值,例如 false.

Move the declaration outside of your if statement, and initialize it to some default value, such as false.

相关文章