keyExists 无法解析为变量 - Java

keyExists cannot be resolved to a variable - Java

我正在做一个 Java 程序,我做了一个方法来检查 Windows 注册表中的键是否存在,该方法看起来像这样

public static void keyExists(String key) throws IOException, InterruptedException {
  
  int returnValue = -1;
  Process process = Runtime.getRuntime().exec("reg query " + key);
  process.waitFor();
  returnValue = process.exitValue();
  
  if(returnValue==0) {
      
      boolean keyExists = true;
      
  }
  
  if(returnValue==1) {
      
      boolean keyExists = false;
      
  }
  
  return keyExists;
  
}

但是 Eclipse IDE 在

中给我错误“keyExists cannot be resolved to a variable”
return keyExists;

我是初学者:)

您只在 if 块中声明了变量 keyExists。编译器不会让您编译该程序,因为在某些情况下您的值将不同于 0 或 1。 而且您没有 return 类型的函数。 请尝试一下

public static boolean keyExists(String key) throws IOException, InterruptedException {

        int returnValue = -1;
        Process process = Runtime.getRuntime().exec("reg query " + key);
        process.waitFor();
        returnValue = process.exitValue();

        return returnValue == 0;
    }