我試圖實現一種方法,它需要一個文本和一列的寬度並輸出文本,每行限於列寬。Java中println()語句的字符數限制
public void wrapText(String text, int width)
{
System.out.println(text);
}
例如,調用與文本的方法是:
Triometric creates unique end user monitoring products for high-value Web applications, and offers unrivalled expertise in performance consulting.
與列寬20會導致下面的輸出:
Triometric creates unique end user monitoring products for high-value Web applications, and offers unrivalled expertise in performance consulting.
迭代字符串的字符並在每個寬度後面添加一個'\ n' –
mm,不是那麼容易@kocko。這可能會導致一個字中間出現分界線! –
這就是說,@ user3182511,如果'width'小於一個字長度會發生什麼?是否將連字符作爲第20個字符插入一行,然後該行的其餘部分是否繼續? –