获得单词的五个连续组合(Getting the five sequential combinations of the words)

编程入门 行业动态 更新时间:2024-10-23 15:17:06
获得单词的五个连续组合(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 divisions

My 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 largest

How 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++; } } }

更多推荐

本文发布于:2023-07-19 01:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169693.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   单词   sequential   combinations   words

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!