【发布时间】:2021-07-14 12:05:32
【问题描述】:
我试图解决这个问题,但遇到了小错误。 我必须用空格重新格式化此文本,并且每行的长度不得超过 60 个字符。
“Java 最初由 Sun Microsystems 的 James Gosling 开发(后来被 Oracle 收购),并于 1995 年作为 Sun Microsystems Java 平台的核心组件发布。”
这是我的代码:
static String text;
public String reformatLines(String text) {
if(text == null || text.isEmpty());
return text;
}
public static void main(String[] args) {
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.";
String str = text.replaceAll("[\n]{2,}", " ");
int length = 60;
String jump = new String();
while(text.length()>length){
int jp = str.lastIndexOf(" ", length);
jump = jump.concat(str.substring(0, jp));
jump = jump.concat("\n");
str = str.substring(jp, str.length());
}
}
}
其实我也不知道怎么办……
【问题讨论】:
标签: java