• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

“找不到符号:变速”尽管声明变量

java 来源:wendy ouyang 4次浏览

当我编译下面的代码,它说:“找不到符号:变速”尽管声明变量

“error: cannot find symbol: variable max”

但我没有定义变量max ….

public class Solution { 
    public static boolean isOneEditDistance(String s, String t) { 
      if (s.length() >= t.length()) { 
        StringBuilder a = new StringBuilder (s); 
        StringBuilder b = new StringBuilder (t); 
        int max = s.length(); 
        int min = t.length(); 
      } 
      else { 
        StringBuilder a = new StringBuilder (t); 
        StringBuilder b = new StringBuilder (s); 
        int max = t.length(); 
        int min = s.length(); 
      } 

      int flag = 0; 

      if ((max-min)>1) 
        return flase; 
      else if ((max-min)==1) { 
       ..... 


===========解决方案如下:

我宁愿如下所示的代码,这也避免了因和问题B是作用域包含在if语句块。

if(s.length() < t.length()){ 
    String h = s; s = t; t = h; 
} 
// now s is not shorter than t 
int max = s.length(); 
int min = t.length(); 
StringBuilder a = new StringBuilder(s); 
StringBuilder b = new StringBuilder(t); 

版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)