Java 此方法必须返回布尔类型的结果

Java 此方法必须返回布尔类型的结果,java,Java,我正在用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.lesCaracte

我正在用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;
      }
    }
  }
}
public boolean mystartwith(MyString s){
如果(s.mylength()>this.mylength()){
返回false;
}否则{
对于(int i=0;i假设
如果(s.mylength()>this.mylength())
不满足,那么代码将进入循环。 现在假设
for
循环没有循环,这意味着
s
为空。将返回什么

完全正确!没有,因为循环将被跳过。

若要解决此问题,您应该在循环后
返回
一些
布尔值

如果
s
为空,将跳过
for
循环,并且您的方法根本不会返回任何内容,因此会出现错误。我宁愿先添加对该条件的检查

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

for (int i=0; i<s.mylength() ; i++){
  if (lesCaracteres[i] != s.lesCaracteres[i]){
    return false;
  } else {
    return true;
  }
}
关键的区别是:
true
只有在正常遍历
for
循环时才会返回(即,通过
i
条件退出)。这反过来意味着
s
字符串的所有字符都与包装的字符串开头的字符匹配。

如果(s.mylength()>this.mylength()){
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
}
返回false; }否则{
对于(int i=0;iJava必须保证无论输入是什么,都会返回布尔值,但是在某些情况下不会返回

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
    }
}
public boolean mystartwith(MyString s){
如果(s.mylength()>this.mylength()){
返回false;
}否则{

对于(int i=0;i您可以这样重新定义代码:

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;
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;
    }
}
public boolean mystartwith(MyString s){
如果(s.mylength()>this.mylength()){
返回false;
}否则{

for(int i=0;i当您在第一次迭代中总是返回false或true时,循环的目的是什么?
i
不能为零以外的任何值。编译器看不到for循环完成后您将返回什么。顺便说一句,您的逻辑是错误的。@marounnaroun情况如何?:)现在它将无法编译,因为
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
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;
    }
}