Java中的递归“Word Warp”程序

时间:2016-03-29 22:21:34

标签: java loops recursion arraylist intellij-idea

我必须编写一个执行以下操作的程序:

  

您的程序将阅读几行文字。您的程序将重新格式化每个   排成较小的行,每行不超过20个字符。如果一个单词会使该行超过20个字符,则整个单词应该移到下一行。单词由一串不包含空格的字符组成。单词可以由字母,数字和/或标点符号组成。您将确保输入中的单词不超过20个字符。输入将包含多行文本。至少会有一个单词   每一行。输入将以包含文本“END”的行结束。您的程序应为输入中的每行文本输出一组格式化的行。空行将分隔每组行。

我已经编写了程序,它正在输出它应该输出的内容,但由于某种原因,它重复了某些行,我无法弄清楚原因。

输入:

The quick brown fox jumped over the lazy dog.
12345678901234567890
Did you see the game in Lane Stadium? 
I hear we won.
LSU fans sob. Virginia Tech fans rejoice. One more victory.
END

我的节目输出:

The quick brown fox
jumped over the lazy
dog.
jumped over the lazy dog.
12345678901234567890
Did you see the game
in Lane Stadium? I
hear we won.
in Lane Stadium? I hear we won.
LSU fans sob.
Virginia Tech fans
rejoice. One more
victory.
rejoice. One more victory.
Virginia Tech fans rejoice. One more victory.

它应该是什么:

The quick brown fox
jumped over the lazy
dog.
12345678901234567890
Did you see the game
in Lane Stadium? I
hear we won.
LSU fans sob.
Virginia Tech fans
rejoice. One more
victory.

这是我的代码:

public static void main(String[] args) {
    String input;
    ArrayList<String> lines = new ArrayList<String>();

    do {
        System.out.println("Enter a line of input or END to exit: ");
        input = console.nextLine();
        if (!input.equalsIgnoreCase("END")) {
            lines.add(input);
        }
    } while (!input.equalsIgnoreCase("END"));

    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i);
        line(line);
    }
}

public static int stopIndex(String s) {
    int stop = 0;
    for (int k = 20; k >= 0; k--) {
        if (s.charAt(k) == ' ') {
            stop = k;
            break;
        }
    }
    return stop;
}

public static void line(String line) {
    String finalLine;
    String finalLine2;
    if (line.length() > 20) {
           if (line.charAt(20) != ' ') {
                int stop = stopIndex(line);
                finalLine = line.substring(0, stop);
                finalLine2 = line.substring(stop + 1, line.length());
            } else {
                finalLine = line.substring(0, 20);
                finalLine2 = line.substring(21, line.length());
            }
    } else {
        finalLine = line.substring(0, line.length());
        finalLine2 = "";
    }
    System.out.println(finalLine);
    if (finalLine2.length() > 20) {
        line(finalLine2);
    }
    if (finalLine2.equals("")) {
        System.out.print(finalLine2);
    } else {
        System.out.println(finalLine2);
    }
}

有人可以告诉我我做错了什么吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

问题出在&#34; line&#34;方法:

    if (finalLine2.length() > 20) {
        line(finalLine2);
    }
    if (finalLine2.equals("")) {
        System.out.print(finalLine2);
    } else {
        System.out.println(finalLine2);
    }  

在这里打印一次(通过调用line方法):

if (finalLine2.length() > 20) {
            line(finalLine2);
}

然后再次打印:

if (finalLine2.equals("")) {
       System.out.print(finalLine2);
} else {
       System.out.println(finalLine2);
}  

尝试将其更改为:

if (finalLine2.length() > 20) {
    line(finalLine2);
}
else {
    System.out.println(finalLine2);
} 

答案 1 :(得分:0)

您的public static void line(String line)方法包含3个println语句。第一个将始终执行,然后第二个或第三个将被执行,因此每个输入行最多可以打印2行。

LSU fans sob. Virginia Tech fans rejoice. One more victory.包含超过2组20个字符的跨度;最多只有两个println来电不会。

您可能想尝试使逻辑使用循环。

提示如下:

  

更改:
     if (line.length() > 20) {
 更喜欢的东西:
     while (line.length() > 20) {
 并且放弃了递归。