风吹过弦

发布于 2024-08-24 04:22:41 字数 627 浏览 7 评论 0原文

我对如何完成这项任务有一些基本的想法,但我不确定我做得是否正确。所以我们有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦初启 2024-08-31 04:22:41

是的,这可能是解决问题的一种简单(不是很高效)的方法。

创建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.

三生路 2024-08-31 04:22:41

最简单的方法是使用正则表达式。这应该是有启发性的:

static String blow(String s) {
    String vowels = "aeiouAEIOU";
    String middle = s.replaceAll("[" + vowels + "]", " ");
    int flip = 0;
    String[] side = { "", "" };
    Scanner sc = new Scanner(s);
    for (String word; (word = sc.findInLine("\\s*\\S*")) != null; ) {
        side[flip] += word.replaceAll(".", " ");
        side[1-flip] += word.replaceAll("[^" + vowels + "]", " ");
        flip = 1-flip;
    }
    return String.format("|%s|%n|%s|%n|%s|", side[0], middle, side[1]);
}

我在输出中添加了 | 字符,以表明它正确处理了多余的空格 - 所有三行都保证相同的长度,处理前导空格、尾随空格,甚至全部空白输入。

如果您不熟悉正则表达式,这绝对是一个很好的学习起点。

middle 只是原始字符串,所有元音都替换为空格。

那么,side[0]side[1] 分别是顶线和底线。我们使用 Scanner 提取每个单词(保留前导和尾随空格)。我们处理每个单词的方式是,一侧全部用空格代替;另一侧全部用空格代替。另一种情况下,只有非元音被空格替换。我们处理的每一个字都会翻转立场。

The simplest way is to use regex. This should be instructive:

static String blow(String s) {
    String vowels = "aeiouAEIOU";
    String middle = s.replaceAll("[" + vowels + "]", " ");
    int flip = 0;
    String[] side = { "", "" };
    Scanner sc = new Scanner(s);
    for (String word; (word = sc.findInLine("\\s*\\S*")) != null; ) {
        side[flip] += word.replaceAll(".", " ");
        side[1-flip] += word.replaceAll("[^" + vowels + "]", " ");
        flip = 1-flip;
    }
    return String.format("|%s|%n|%s|%n|%s|", side[0], middle, side[1]);
}

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] and side[1] are the top and bottom lines respectively. We use the Scanner 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文