有一个任务。该方法需要一个字符串(长),并且必须逐行打印,以便每个输出行的长度不超过 13 个字符并且单词不会被破坏(仅在空格上换行)。我是通过循环来完成的,但不知何故它非常可怕,我觉得我可以使用递归,但我不知道如何使用。
示例输入
Four score and seven years ago our fathers brought forth upon this continent a new nation,conceived in liberty and dedicated to the proposition that all men are created equal
结论
Four score
and seven
years ago
our fathers
brought
forth upon
this
continent a
new nation,
conceived in
liberty and
dedicated to
the
proposition
that all men
are created
equal
这是一个可能的递归解决方案:
但是,如果您有机会使用第三方库,那么我建议您采取
WordUtils#wrap
:更短更容易。我想指出,该实现
WordUtils#wrap
使用循环,而不是递归,而且这种实现非常“可怕”。