有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

字符串的java检索方法

嘿,伙计们,我得到了这个密码:

String arch = System.getenv("PROCESSOR_ARCHITECTURE");
String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");
String realArch = arch.endsWith("64")
    || wow64Arch != null && wow64Arch.endsWith("64")
       ? "64" : "32";

public String officeLoc() throws IllegalArgumentException, InvocationTargetException, IllegalAccessException{
if(realArch.contains("64")){
    String tempLoc = WinRegistry.readString (
        WinRegistry.HKEY_LOCAL_MACHINE,                     //HKEY
        "SOFTWARE\\Wow6432Node\\Microsfot\\Office",         //Key
        "InstallDir");                                      //ValueName
    System.out.println("Location = " + tempLoc);
    }else{
    String tempLoc = WinRegistry.readString (
        WinRegistry.HKEY_LOCAL_MACHINE,                     //HKEY
        "SOFTWARE\\Microsoft\\Office\\",                    //Key
        "InstallDir");                                      //ValueName
    System.out.println("Location = " + tempLoc);
    }
}

我无法将tempLoc值返回到officeLoc。我尝试使用返回字符串x;甚至是静态的,但它不是这样工作的。 我做错了什么


共 (2) 个答案

  1. # 1 楼答案

    你可能会学习java语法的规则。因为:除非你的代码示例中有拼写错误,否则你的问题只是你的方法

    public String officeLoc() ...
    

    他错过了决赛

      return tempLoc;
    }
    

    声明!(好吧,但正如Jason在回答中提到的那样:只有在该方法的开头准确地声明了tempLoc一次,这才有效

    为了它,你会重写整件事

    public String officeLoc() throws IllegalArgumentException, InvocationTargetException, IllegalAccessException{
      if(realArch.contains("64")) {
        return lookupRegistry("SOFTWARE\\Wow6432Node\\Microsfot\\Office");
      }
    
      return lookupRegistry("SOFTWARE\\Microsfot\\Office");                                       
    }
    

    还有一个助手方法lookupRegistry(字符串键),可以

    return WinRegistry.readString (
        WinRegistry.HKEY_LOCAL_MACHINE, key, "InstallDir");                                      
    

    你看:你绝对想要最小化代码重复的数量

  2. # 2 楼答案

    还有其他方法可以改进代码,但要关注手头的问题:您的困惑可能集中在tempLocscope上。如果你有这样的东西:

    public String officeLoc () {
        if (...) {
            String tempLoc = ...;
        } else {
            String tempLoc = ...;
        }
    }
    

    然后,这两个不同的变量的范围仅在它们周围的{}之间^一旦超出范围,{}就不再可见。因此,你无法做到这一点(听起来你已经尝试过了):

    public String officeLoc () {
        if (...) {
            String tempLoc = ...;
        } else {
            String tempLoc = ...;
        }
        return tempLoc; // <- can't do this, tempLoc is out of scope
    }
    

    基本上,你有两个选择。你可以这样做:

    public String officeLoc () {
        if (...) {
            String tempLoc = ...;
            return tempLoc; // <- no problem
        } else {
            String tempLoc = ...;
            return tempLoc; // <- no problem
        }
    }
    

    或者您可以将tempLoc移到更高的范围,如下所示:

    public String officeLoc () {
        String tempLoc; // <- declare it here
        if (...) {
            tempLoc = ...; // <- set its value
        } else {
            tempLoc = ...; // <- set its value
        }
        return tempLoc; // <- no problem
    }
    

    无论哪种方式,它都需要在return语句所在的范围内可见。就个人而言,我更喜欢后者,因为作为一种风格选择,我喜欢最小化返回点,但这取决于你

    你可能还想读一点关于^{}的书,因为你试图使用它表明你可能不完全理解它的实际用途