public class BruteForceSearch {
private char[] Text;
private char[] MyWord;
private int TextLength;
private int MyWordLength;
//word or -1 if not found
public int search(String Text, String MyWord) {
//chars
this.Text = Text.toCharArray();
this.MyWord = MyWord.toCharArray();
this.TextLength = Text.length();
this.MyWordLength = MyWord.length();
for (int TextCounter = 0; TextCounter < TextLength - MyWordLength; TextCounter++) {
int WordCounter = 0;
//matched increament WordCounter
while (WordCounter < MyWordLength && this.Text[TextCounter + WordCounter] == this.MyWord[WordCounter]) {
WordCounter++;
}
if (WordCounter == MyWordLength) {
return TextCounter;
}
}
// return -1 in case you didn't find the word
return -1;
}
在这里我的问题是什么是这些循环 为什么开始和结束这样 for循环(TextCounter < TextLength - MyWordLengt)
while循环的点(while (WordCounter < MyWordLength && this.Text[TextCounter + WordCounter] == this.MyWord[WordCounter]
)我看不到这个代码两行的点
你问for循环什么是?我没有看到你对此感到困惑。 – Gendarme
我很担心我的愚蠢的问题,抱歉,但我认为人们会帮我althoughmy愚蠢:d但我问的逻辑不是这样,对于循环工作 –