风吹过弦
我对如何完成这项任务有一些基本的想法,但我不确定我做得是否正确。所以我们有 WindyString 类,其方法为打击方法。使用它之后:
System.out.println(WindyString.blow(
"Abrakadabra! The second chance to pass has already BEGUN! "));
我们应该得到这样的结果:
e a e a a ea y
br k d br ! Th s c nd ch nc t p ss h s lr d B G N!
A a a a a e o o a E U
所以简而言之,在每个第二个单词中,我们选择每个元音并将它们移到上面一行。在单词的后半部分,我们将元音移到下面一行。
我知道我应该使用 tokenizer 或 split 方法将字符串拆分为标记,但接下来该怎么办?创建 3 个数组,每个数组代表每一行?
I have some basic idea on how to do this task, but I'm not sure if I'm doing it right. So we have class WindyString with metod blow. After using it :
System.out.println(WindyString.blow(
"Abrakadabra! The second chance to pass has already BEGUN! "));
we should obtain something like this :
e a e a a ea y
br k d br ! Th s c nd ch nc t p ss h s lr d B G N!
A a a a a e o o a E U
so in a nutshell in every second word we pick every vowels and move them one line above. In the second half of words we move vowels one line below.
I know I should split string to tokens with tokenizer or split method,but what next ? Create 3 arrays each representing each row ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这可能是解决问题的一种简单(不是很高效)的方法。
创建3个数组;一个用实际数据填充,两个数组用
' '
填充 (Arrays.fill
)。然后迭代包含实际数据的数组,并保留当前所在单词的整数和布尔值(如果已经匹配空格)。
迭代时,您检查该字符是否是元音。如果它是元音,请检查字数(奇数/偶数)并将其放入第一个或第三个数组中。
当到达空格时,设置布尔值并增加字数。如果到达另一个空格,请检查该空格是否已设置:如果是,则继续。如果匹配非空白,则重置空白布尔值。
然后将所有数组连接在一起,并在每个连接的数组之间附加一个换行符并返回字符串。
Yes, that's probably an easy (not very performant) way to solve the problem.
Create 3 arrays; one is filled with the actual data and 2 arrays are filled (
Arrays.fill
) with' '
.Then iterate over the array containing the actual data, and keep an integer of which word you're currently at and a boolean if you already matched whitespace.
While iterating, you check if the character is a vowel or not. If it's a vowel, check the word-count (oddness/evenness) and place it in the first or third array.
When you reach a whitespace, set the boolean and increase the word count. If you reach another whitespace, check whether the whitespace is already set: if so, continue. If you match a non-whitespace, reset the whitespace boolean.
Then join all arrays together and append a new-line character between each joined array and return the string.
最简单的方法是使用正则表达式。这应该是有启发性的:
我在输出中添加了
|
字符,以表明它正确处理了多余的空格 - 所有三行都保证相同的长度,处理前导空格、尾随空格,甚至全部空白输入。如果您不熟悉正则表达式,这绝对是一个很好的学习起点。
middle
只是原始字符串,所有元音都替换为空格。那么,
side[0]
和side[1]
分别是顶线和底线。我们使用Scanner
提取每个单词(保留前导和尾随空格)。我们处理每个单词的方式是,一侧全部用空格代替;另一侧全部用空格代替。另一种情况下,只有非元音被空格替换。我们处理的每一个字都会翻转立场。The simplest way is to use regex. This should be instructive:
I added the
|
characters in the output to show that this processes excess whitespaces correctly -- all three lines are guaranteed the same length, taking care of leading blanks, trailing blanks, or even ALL blanks input.If you're not familiar with regular expressions, this is definitely a good one to start learning with.
The
middle
is simply the original string with all vowels replaced with spaces.Then,
side[0]
andside[1]
are the top and bottom lines respectively. We use theScanner
to extract every word (preserving leading and trailing spaces). The way we process each word is that in one side, everything is replaced by blanks; in the other, only non-vowels are replaced by blanks. We flip sides with every word we process.