【问题标题】:this method must return a result of type boolean此方法必须返回布尔类型的结果
【发布时间】:2013-11-24 18:35:28
【问题描述】:

我正在用 java 重写类字符串,但是对于像 startwith 这样的方法,我有同样的错误。这是我的代码:

public boolean mystartwith(MyString s){
    if(s.mylength() > this.mylength()){
    return false;
    }else{
    for(int i=0 ; i<s.mylength() ; i++){
        if(lesCaracteres[i] != s.lesCaracteres[i]){
            return false;
        }else{
        return true;
      }
    }
  }
}

我有这个错误:“这个方法必须返回布尔类型的结果”

【问题讨论】:

  • 第一次迭代总是返回 false 或 true 时,循环的目的是什么? i 只能是零。
  • 编译器看不到你在 for 循环结束后会返回什么。顺便说一句,你的逻辑是错误的。

标签: java


【解决方案1】:

如果s 为空,for 循环将被跳过 - 您的方法根本不会返回任何内容,因此会出现错误。我宁愿先添加对这种情况的检查。

但必须注意,给定的算法存在缺陷:

for (int i=0; i<s.mylength() ; i++){
  if (lesCaracteres[i] != s.lesCaracteres[i]){
    return false;
  } else {
    return true;
  }
}

好的,假设我用s 给出的'abc' 字符串调用了这个函数,但实例包裹在字符串'acdef' 上。你猜怎么着,你的方法会返回true!问题是您的循环中断得太快:在检查第一个字母后立即返回一个值。

其实应该是这样写的:

int sLength = s.myLength();
if (sLength == 0) {
  return false; 
  // actually, it's for you to decide: 
  // technically each string begins with an empty string
}
if (sLength > this.mylength()) {
  return false;
}
for (int i = 0; i < sLength; i++) {
  if (lesCaracteres[i] != s.lesCaracteres[i]){
    return false;
  }
}
return true;

关键区别:true 仅在for 循环正常遍历(即通过i &lt; sLength 条件退出)时返回。这反过来意味着,所有字符的s 字符串匹配那些包裹在字符串开头的字符串。

【讨论】:

    【解决方案2】:

    假设if(s.mylength() &gt; this.mylength()) 不满足,那么你的代码会进入循环。 现在假设for 循环没有循环,这意味着s 是空的。会退回什么?

    没错!什么都没有,因为循环将被跳过。
    要解决此问题,您应该在循环之后 return 一些 boolean

    【讨论】:

      【解决方案3】:

      你可以像这样重新定义你的代码:

      public boolean mystartwith(MyString s){
          if(s.mylength() > this.mylength()){
             return false;
          }else{
             for(int i=0 ; i<s.mylength() ; i++){
                if(lesCaracteres[i] != s.lesCaracteres[i]){
                   return false;
                }else{
                   return true;
                }
             }
             return false;
          }
      }
      

      【讨论】:

        【解决方案4】:

        Java 必须保证无论输入什么都会返回一个布尔值,但是在某些情况下没有返回

        public boolean mystartwith(MyString s){
            if(s.mylength() > this.mylength()){
                return false;
            }else{
                for(int i=0 ; i<s.mylength() ; i++){
                    if(lesCaracteres[i] != s.lesCaracteres[i]){
                        return false;
                    }else{
                        return true;
                    }
                }
                //what if s.mylength()==0 and the loop never runs?!
                //therefore the code will get to here, and the method will end without a return
            }
        }
        

        请注意,java 并没有“足够聪明”来识别两个 if 一起产生所有可能性。例如

          boolean method(int a){
            if (a>0){
                return true;
            }else{
        
               if (a<=0){
                  return false;
               }
               //program can never get here, BUT java doesn't realise that
          }
        

        仍然不满足 java,因为它设想了一个两个 if 都为假的场景

        如何解决这个问题取决于你的具体程序,但值得注意的是 for 循环在返回之前最多只运行一次,所以是多余的

        【讨论】:

          【解决方案5】:
          if(s.mylength() > this.mylength()){
              return false;
          }else{
              for(int i=0 ; i<s.mylength() ; i++){
                  if(lesCaracteres[i] != s.lesCaracteres[i]){
                      return false;
                  }else{
                  return true;
                }
              }
              return ____; //place true or false based on your condition
          }
          

          【讨论】:

          • @MarounMaroun 怎么样? :)
          • 现在它不会编译因为____.. :D
          • 只需用 true 或 flase 填充该空白即可! :)
          猜你喜欢
          • 2020-08-08
          • 2013-12-18
          • 2015-09-25
          • 1970-01-01
          • 2020-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多