获得单词的五个连续组合(Getting the five sequential combinations of the words)
java
所以我试着得到五个连续的单词。 我有这个输入:
太平洋是地球上最大的海洋分界
输出应该像:
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怎么解决?
So I am trying to get the five sequential of the words. I have this input:
Pacific Ocean is the largest of the Earth's oceanic divisions
Output should be like:
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 divisionsMy attempt:
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++; } } }But the output is wrong! Output:
Pacific Pacific Ocean Pacific Ocean is Pacific Ocean is the Pacific Ocean is the largestHow to fix it?
最满意答案
更改片段count = 0的位置count = 0 :
public void getComb(String line) { String words[] = line.split(" "); for (int i = 0; i < words.length; i++) { int count = 0; // RESET COUNT String word = ""; int m = i; while (count < 5 && m < words.length) { // NO EXCEPTION with 'm' limit count++; word += " " + words[m]; System.out.println(word); m++; } } }Change the position of the snippet count = 0:
public void getComb(String line) { String words[] = line.split(" "); for (int i = 0; i < words.length; i++) { int count = 0; // RESET COUNT String word = ""; int m = i; while (count < 5 && m < words.length) { // NO EXCEPTION with 'm' limit count++; word += " " + words[m]; System.out.println(word); m++; } } }更多推荐
发布评论