【问题标题】:Learning java, cannot find symbol学习java,找不到符号
【发布时间】:2012-10-18 04:21:08
【问题描述】:

我正在学习 Java,并坚持进行自测练习,编写一个向后打印字符串的递归函数...

我了解编译器错误,但我不知道该怎么办。

我的代码...

class Back {
    void Backwards(String s) {
            if (s.length = 0) { 
                    System.out.println();
                    return;
            }
            System.out.print(s.charAt(s.length));
            s = s.substring(0, s.length-1);
            Backwards(s);
    }
}

class RTest {
    public static void main(String args[]) {
            Back b;
            b.Backwards("A STRING");
    }

}

编译器输出...

john@fekete:~/javadev$ javac Recur.java 
Recur.java:3: error: cannot find symbol
    if (s.length = 0) { 
         ^
  symbol:   variable length
  location: variable s of type String
Recur.java:7: error: cannot find symbol
    System.out.print(s.charAt(s.length));
                               ^
  symbol:   variable length
  location: variable s of type String
Recur.java:8: error: cannot find symbol
    s = s.substring(0, s.length-1);
                        ^
  symbol:   variable length
  location: variable s of type String
3 errors

完成的代码...

class Back {
    static void backwards(String s) {
            if (s.length() == 0) {
                    System.out.println();
                    return;
            }
            System.out.print(s.charAt(s.length()-1));
            s = s.substring(0, s.length()-1);
            backwards(s);
    }
}

class RTest {
    public static void main(String args[]) {

            Back.backwards("A STRING");
    }
}   

【问题讨论】:

  • 请遵守 Java 命名约定:方法以小写字母开头。

标签: java string recursion


【解决方案1】:

这样写:

s.length() == 0 // it's a method, not an attribute

【讨论】:

    【解决方案2】:

    你忘记了括号:

    s.length()
    

    【讨论】:

      【解决方案3】:

      lengthmethod,而不是属性。你必须这样使用它:

      s.length(); // note the use of parens
      

      另外,由于以下情况,修复后会出现编译错误:

      if (s.length = 0) { 
      

      应该是

      if (s.length == 0) { 
      

      最后,在您的 main 方法中,b 变量必须被实例化,使用

      Back b = new Back();
      

      【讨论】:

      • 只回答问题,不要为我修复所有错误而破坏学习体验。此外,还有更多的问题。
      • 很抱歉破坏了这次体验。请放心,我不会犯同样的错误两次。
      【解决方案4】:

      在您的 if 语句中,您将 0 分配给 s.length 而不是检查。这样做:

      if(s.length()==0)
      //rest of your code
      

      另一个错误是s.charAt(s.length())。字符串中第 i 个字符的索引为 (i-1),类似于数组的索引。所以字符串的最后一个字符有索引(s.length()-1)。所以用s.charAt(s.length()-1)替换那行代码。

      【讨论】:

        【解决方案5】:

        这应该更好地反映你想要完成的事情:

        class Back {
            void Backwards(String s) {
                    if (s.length() == 0) { 
                            System.out.println();
                            return;
                    }
                    System.out.print(s.charAt(s.length()));
                    s = s.substring(0, s.length()-1);
                    Backwards(s);
            }
        }
        
        public class RTest {
            public static void main(String args[]) {
                    Back b = new Back();
                    b.Backwards("RAPE APE");
            }
        }
        
        • length() 是一个函数
        • 比较使用==
        • 您必须实例化 b 才能使用它

        【讨论】:

          【解决方案6】:

          - 对于String,我们提供了一个名为length()函数,而不是一个字段length

          -如果您使用的是Array,那么它应该是length,因为数组只有一个实例变量名为length

          例如:

          s.length() == 0;
          

          【讨论】:

            【解决方案7】:

            一些通用的“良好编码”建议:

            • 类名应该代表一个“事物”,通常类名是一个名词(例如“StringTool”)
            • 方法应该代表一个动作,通常方法名是一个动词(例如“reverse”)
            • 参数和变量名称应该有意义并描述它们所代表的内容。
            • 您不应重新分配方法参数,因为这可能会产生误导。
            • 一个方法应该有一个确切的责任(所以不要反转和打印一个字符串)。这促进了清晰度和重用性。

            我已将这些建议应用到您完成的代码中,见下文:

            public class StringTool {
            
                public static String reverse(String source) {
            
                    // stop condition of the recursion
                    if (source.isEmpty()) {
                        return "";
                    }
            
                    int lastPosition = source.length() - 1;
                    String lastCharacter = source.charAt(lastPosition);
                    String restOfSource = source.substring(0, lastPosition);
            
                    // place the last character at the beginning and reverse the rest 
                    // of the source recursively
                    return lastCharacter + reverse(restOfSource);
                }
            
                // test method
                public static void main(String args[]) {
                    System.out.println(reverse("A STRING"));
                }
            
            } 
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-04-18
              • 2013-09-05
              • 1970-01-01
              • 2020-07-02
              • 2015-09-20
              • 1970-01-01
              相关资源
              最近更新 更多