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但我問的邏輯不是這樣,對於循環工作 –