所有代码都挤在公共静态无效主体中。创建至少两个具有直观名称的方法,以解释它们如何使其更具可读性。这样我就可以浏览一下,看看你在做什么的主要主题。
您的 for 循环在循环的每次迭代中检查数组的大小。每次迭代都重新扫描阵列是浪费处理器时间。
不要将类称为“main”,而是给它一个名称来说明它的作用。像“SetFormattedSpacingWidth”或捕捉到它的本质的东西。
有一个三重嵌套的 for 循环。三重嵌套的 for 循环通常可以以多态方式重写为只有一个或最多两个 for 循环。
您正在使用 for 循环的旧方式,而不是这样:
for(int b = 0; b < wordsOfLine.size(); b++) {
line += wordsOfLine.get(b);
//access words via: wordsOfLine.get(b);
}
使用新的java foreach 循环减去一行并使其更具可读性。
for(String words : wordsOfLine) {
//access words via: 'words'
}
i--
在递增 i 的 for 循环内递减?这真是令人困惑。
可能会发生除以零异常。你甚至不需要抓住那些。它们应该是不可能发生的。
这是我对这个有趣问题的尝试:
import java.io.IOException;
import java.util.ArrayList;
public class DoFormattedColumnWidth {
public static void main(String[] args) {
String msg = "Contrary to popular belief, Lorem Ipsum is not simply " +
"random text. It has roots in a piece of classical Latin " +
"literature from 45 BC, making it over 2000 years old. Richard " +
"McClintock, a Latin professor at Hampden-Sydney College in " +
"Virginia, looked up one of the more obscure Latin words, " +
"consectetur, from a Lorem Ipsum passage, and going through the " +
"cites of the word in classical literature, discovered the " +
"undoubtable source. Lorem Ipsum comes from sections 1.10.32 and " +
"1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good " +
"and Evil) by Cicero, written in 45 BC. This book is a treatise on " +
"the theory of ethics, very popular during the Renaissance. The first " +
"line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a " +
"line in section 1.10.32.";
ArrayList<String> lines =
justifyText(msg, 25);
for(String line : lines){
System.out.println(line);
}
}
public static ArrayList<String> justifyText(String text, int width){
ArrayList<String> lines = new ArrayList<String>();
String[] words = text.split(" ");
String currentLine = "";
int currentWord = 0;
int len = words.length;
int numberOfWordsThisLine=0;
while (currentWord < len){
if ((currentLine.length() + words[currentWord].length()) <= width ||
(currentLine.length() + words[currentWord].length()) > width &&
numberOfWordsThisLine == 0){
currentLine = currentLine + " " + words[currentWord];
currentWord++;
numberOfWordsThisLine++;
if (currentWord == len)
lines.add(currentLine.trim());
}
else{
currentLine = infuseLineWithSpaces(currentLine.trim(),
width - (currentLine.trim().length()));
lines.add(currentLine.trim());
currentLine = "";
numberOfWordsThisLine=0;
}
}
return lines;
}
public static String infuseLineWithSpaces(String text, int numSpaces){
String newText = "";
if (numSpaces == 0) return text;
else if (numSpaces == 1) return text + " ";
else if (numSpaces > 1){
String[] words = text.split(" ");
int numberOfWords = words.length;
int left = (numSpaces + (numberOfWords-1));
int right = ((words.length-1));
int numberSpacesToAddEachWord = (int)((double)left / (double)right);
for(int x = 0; x < numberOfWords; x++){
if (x == numberOfWords)
newText = newText + words[x];
else
newText = newText + words[x] + getSpaces(numberSpacesToAddEachWord);
}
}
else
return text;
return newText;
}
public static String getSpaces(int spaces){
String text = "";
for(int x = 0; x < spaces; x++){
text+= " ";
}
return text;
}
}
哪个输出:
Contrary to popular
belief, Lorem Ipsum is
not simply random text.
It has roots in a piece
of classical Latin
literature from 45 BC,
making it over 2000 years
old. Richard McClintock,
a Latin professor at
Hampden-Sydney College in
Virginia, looked up one
of the more obscure Latin
words, consectetur, from
a Lorem Ipsum passage,
and going through the
cites of the word in
classical literature,
discovered the
undoubtable source. Lorem
Ipsum comes from sections
1.10.32 and 1.10.33 of
"de Finibus Bonorum et
Malorum" (The Extremes of
Good and Evil) by Cicero,
written in 45 BC. This
book is a treatise on the
theory of ethics, very
popular during the
Renaissance. The first
line of Lorem Ipsum,
"Lorem ipsum dolor sit
amet..", comes from a
line in section 1.10.32.
这并不完美,因为单词之间的间距是由一行中的单词数除以填补空白所需的额外空格数来确定的,因此正确的行并不完全合理。但这比以前更有道理。