Итак, я пытаюсь получить пять последовательных слов. У меня есть этот вход:Получение пяти последовательных комбинаций слов
Тихий океан является самым крупным из океанических подразделений Земли
Выхода должен быть как:
Pacific
Pacific Ocean
Pacific Ocean is
Pacific Ocean is the
Pacific Ocean is the largest
Ocean
Ocean is
Ocean is the
Ocean is the largest
Ocean is the largest of
is
is the
is the largest
is the largest of
is the largest of the
the
the largest
the largest of
the largest of the
the largest of the Earth's
largest
largest of
largest of the
largest of the Earth's
largest of the Earth's oceanic
of
of the
of the Earth's
of the Earth's oceanic
of the Earth's oceanic divisions
the
the Earth's
the Earth's oceanic
the Earth's oceanic divisions
Earth's
Earth's oceanic
Earth's oceanic divisions
oceanic
oceanic divisions
divisions
Моей попытка:
public void getComb(String line) {
String words[] = line.split(" ");
int count = 0;
for (int i = 0; i < words.length; i++) {
String word = "";
int m = i;
while (count < 5) {
count++;
word += " " + words[m];
System.out.println(word);
m++;
}
}
}
Но результат неправильный! Выход:
Pacific
Pacific Ocean
Pacific Ocean is
Pacific Ocean is the
Pacific Ocean is the largest
Как это исправить?
Это будет бросить ArrayIndexOutOfBoundsException, когда вы достигнете последних четырех слов ... – jpw
да. Я выхожу за пределы индекса 10 –
Это исправлено! – Andynedine