这是我为编码蝙蝠项目写的。出于某种原因,它说这种方式不起作用,但如果我翻转它,它就会起作用。这是为什么?当它输入少于 3 个字符的内容时,它会根据 codebat 收到一条错误消息。
// Given a string, return a new string where "not " has been added to the front.
// However, if the string already begins with "not", return the string unchanged.
// Note: use .equals() to compare 2 strings.
// notString("candy") → "not candy"
// notString("x") → "not x"
// notString("not bad") → "not bad"
public String notString(String str) {
String m;
if ( str.substring (0,3).equalsIgnoreCase("not") && str.length () >= 3 ) // this line doesn't work in it's current state
// but works if I flip the two boolean expressions over the &&
m = str;
else {
m = "not " + str;
}
return m;