該程式旨在從字串中洗掉雙倍、三倍...空格并將字符限制為每行 60 個。
問題是當它必須跳過它們時,它會將單詞放入第 60 個位置的行中。
輸出是:
Java was originally developed by James Gosling at Sun Microsystems
(which has since been acquired by Oracle) and released in 1995
as a core component of Sun Microsystems' Java platform.
但正確的輸出必須是:
Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems'
Java platform.
到目前為止我的代碼:
StringBuilder sb = new StringBuilder(text.replaceAll("(^ )|( $)", "").replaceAll("\\s{2,}", " "));
int i = 0;
while ((i = sb.indexOf(" ", i 60)) != -1) {
sb.replace(i,i 1,"\n");
}
String result = sb.toString();
return result;
}
uj5u.com熱心網友回復:
這是我第 24 次測驗運行的結果。
Java was originally developed by James Gosling at Sun Microsystems
(which has since been acquired by Oracle) and released in 1995
as a core component of Sun Microsystems' Java platform.
Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems'
Java platform.
第一段是輸入。第二段是輸出。
看一下代碼。你看到幾個注釋掉的System.out.println陳述句嗎?這就是您發現代碼問題的方式。您撰寫幾行代碼,然后列印中間結果。沖洗并重復,直到您的代碼有效。
這是完整的可運行代碼。
public class FixedLengthLines {
public static void main(String[] args) {
new FixedLengthLines().processString();
}
public void processString() {
String text = "Java was originally developed by James Gosling at Sun Microsystems\r\n"
"(which has since been acquired by Oracle) and released in 1995\r\n"
"as a core component of Sun Microsystems' Java platform.";
String temp = text.replaceAll("\\s{2,}", " ");
// System.out.println(temp);
int lineLength = 60;
int lineIndex = 0;
int endIndex = Math.min(lineIndex lineLength, temp.length());
StringBuilder builder = new StringBuilder();
while (lineIndex < endIndex) {
// System.out.println(lineIndex " " endIndex " " temp.length());
if (endIndex >= lineIndex lineLength) {
endIndex = temp.lastIndexOf(" ", endIndex);
}
builder.append(temp.substring(lineIndex, endIndex));
builder.append(System.lineSeparator());
// System.out.println(endIndex " " temp.substring(lineIndex, endIndex));
lineIndex = endIndex 1;
endIndex = Math.min(lineIndex lineLength, temp.length());
}
System.out.println(text);
System.out.println();
System.out.println(builder.toString());
}
}
uj5u.com熱心網友回復:
干得好。更換后。效率不高,但應該能解決問題。
int pos = 0;
StringBuilder stringBuilder = new StringBuilder(text);
String finalText = recursiveBuild(stringBuilder, pos).toString();
System.out.println(finalText);
}
private static StringBuilder recursiveBuild(StringBuilder stringBuilder, int pos) {
if (pos 60<stringBuilder.length()){
if (pos == 0) pos =60;
int tempPos = stringBuilder.substring(0, pos).lastIndexOf(" ");
stringBuilder.delete(tempPos, tempPos 1).insert(tempPos, "\n");
stringBuilder = recursiveBuild(stringBuilder, tempPos 60);
} else {
int tempPos = stringBuilder.substring(0, pos).lastIndexOf(" ");
stringBuilder.delete(tempPos, tempPos 1).insert(tempPos, "\n");
}
return stringBuilder;
}
只適用于
String text = "Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995 as a core component of Sun Microsystems' Java platform.";
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/323627.html
下一篇:獲取下劃線前的字符
