我制作了一个程序来测试子字符串是否是另一个字符串的一部分。但它总是返回false
。
public class SubString{
public static void main(String... args){
String findFrom = "mouse";
String toFind = "mouse and cat";
boolean flag = false;
int toFindLenght = toFind.length();
int findFromLength = findFrom.length();
for (int x = 0; x < toFindLenght; x++) {
char toFindIntermediate = toFind.charAt(x);
for (int y = 0; y < findFromLength; y++) {
char toFindFromIntermediate = findFrom.charAt(y);
int counter = x;
if (toFindIntermediate == toFindFromIntermediate) {
toFindFromIntermediate=toFind.charAt(counter);
counter++;
flag=true;
} else {
flag=false;
}
}
}
System.out.println("IS the substring a part of the string :"+flag);
}
}
答案 0 :(得分:2)
这是因为你没有突破循环,所以即使条件在一个周期内被设置为true
,它也会在另一个周期中重置为false
。
答案 1 :(得分:0)
我在这里张贴了相同的代码,但变化很小。
here what i have done is i just change the y counter of inner for loop to start from counter and added a break after flag turs true.
很抱歉,我没有查看否定案例。这里我发布了编辑代码
public class SubString {
public static void main(String... args) {
String findFrom = "mouse";
String toFind = "mouse and cat";
boolean flag = false;
int toFindLenght = toFind.length();
int findFromLength = findFrom.length();
int counter = 0;
for (int x = 0; x < toFindLenght; x++) {
char toFindIntermediate = toFind.charAt(x);
for (int y = counter; y < findFromLength; y++) {
char toFindFromIntermediate = findFrom.charAt(y);
if(toFindIntermediate == ' '){
break;
}
else if (toFindIntermediate == toFindFromIntermediate) {
toFindFromIntermediate = toFind.charAt(counter);
counter++;
flag = true;
break;
} else if (0 != counter) {
flag = false;
counter = 0;
break;
}
}
}
System.out.println("IS the substring a part of the string :" + flag);
}
}
答案 2 :(得分:0)
我不喜欢使用break in循环,我最初的学习方法是猜测,除非有一些原因我不能通过1次额外的布尔测试来减少性能,我会避免它。以下是您的代码,其中包含已更改的注释:
public class SubString {
public static void main(String... args) {
String findFrom = "mouse";
String toFind = "mouse and cat";
boolean flag = false;
int toFindLength = toFind.length();
int findFromLength = findFrom.length(); // fixed spelling of length
for (int x = 0; x <= toFindLength - findFromLength && !flag; x++) {
// Changed the test in the for loop above so you won't get
// index-out-of-bounds exceptions in the next loop.
// char toFindIntermediate = toFind.charAt(x); // Moving this lower
flag = true; // this lets you avoid break
for (int y = 0, counter = x; y < findFromLength && flag; y++) {
char toFindIntermediate = toFind.charAt(counter);
char findFromIntermediate = findFrom.charAt(y);
// took "to" off above variable name for consistancy
// int counter = x;
if (toFindIntermediate == findFromIntermediate) {
// When you change the name of the variable you
// notice pretty quickly the assignment below is wrong.
// Also, you need to increment counter before you store
// the next char
// findFromIntermediate=toFind.charAt(counter);
// counter++;
// flag=true;
counter++;
} else {
flag=false;
}
}
}
System.out.println("IS the substring a part of the string :"+flag);
}
}
没有评论,所以一次看到更容易一点:
public class SubString {
public static void main(String... args) {
String findFrom = "mouse";
String toFind = "mouse and cat";
boolean flag = false;
int toFindLength = toFind.length();
int findFromLength = findFrom.length();
for (int x = 0; x <= toFindLength - findFromLength && !flag; x++) {
flag = true;
for (int y = 0, int counter = x; y < findFromLength && flag; y++) {
char toFindIntermediate = toFind.charAt(counter);
char findFromIntermediate = findFrom.charAt(y);
if (toFindIntermediate == findFromIntermediate) {
counter++;
} else {
flag=false;
}
}
}
System.out.println("IS the substring a part of the string :"+flag);
}
}
这应该做你想要的,同时非常坚持你的原始方法。我不会向任何人展示这一点。首先,你的变量名称是向后的 - 通常“toFind”将是你在更大的变量中寻找的子串。此外,您需要清理循环中初始化和递增变量的方式,以便在整个代码中保持一致。你甚至可以切换到while循环并让它变得清晰,如果不是更多,或者转向另一个方向并将内部循环中的所有内容移动到循环声明中,并且内部没有任何内容。
无论如何,这应该为你提供一个工作基础来构建。