Google의 일부 솔루션을 사용하여 코드를 작성했습니다. while 루프가하는 일에 대해 자세히 도와 주시겠습니까?문자열과 하위 문자열 및 기본 문자열에있는 하위 문자열의 수
import java.util.Scanner;
public class TwoStringsWordRepeat {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
System.out.print("Enter Sentence: ");
String sentence = s.nextLine();
System.out.print("Enter word: ");
String word = s.nextLine();
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = sentence.indexOf(word, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += word.length();
}
}
System.out.println(count);
}
}
while 루프 .indexOf();
의 코드를 설명해주세요.
indexOf()는 urs의 코드 블록에서 사용 된 변수에 따라'lastIndex' 위치에서 검색을 시작하여'sentence'에서'word'의 첫 번째 항목을 찾습니다. – Adarsh