所有的代碼都擠在public static void main中。用直觀的名稱創建至少兩個方法,解釋他們如何使其更具可讀性。這樣我可以瀏覽一下,看看你在做什麼的主要主題。
您的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 loop減去一行,並使其更具可讀性。
for(String words : wordsOfLine) {
//access words via: 'words'
}
遞減i--
INSIDE一個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.
它不是完美的,因爲單詞之間的間距被確定爲號碼的字數除以需要多少額外空間才能填補空白,所以右行不是完全合理的。但比以前更合理。
聽起來就像你一次尋找各種事情的建議。你所問的一個具體問題似乎與字母寬度有關,這只是你的控制檯字體問題,而不是你的代碼。 –