对于我们的Java类,我们必须使用L allign
,Centered
格式化文本,R allign.
文本格式只使用字符串
我已经找到了如何将字符串保存到一排,但我不知道如何重置该行。
任何帮助将是伟大的!
public static String word(String text, int width) {
String word = "";
String all = "";
String row = "";
String newRow = "";
int textlen = text.length();
for (int i = 0; i < text.length(); i++) {
char current = text.charAt(i);
// works gives a word
do {
if (text.charAt(i) != ' ') {
word += text.charAt(i);
}
i++;
} while (text.charAt(i) != ' ' && i < textlen);
// till here
word += " ";
// System.out.println("this is word: " +word);
while (row.length() < width) {
if (word.length() < width
&& (row.length() + word.length()) < width) {
row += word;
if ((width - (row.length() + word.length())) < 3) {
System.out.println("here is row: " + row);
}
}
if (word.length() > width
&& (row.length() + word.length()) > width) {
}
word = "";
}
row = "";
}
return word;
}
我不明白,为什么你有开始循环'而(row.length()<宽度){'。在代码的这一点上,你有一个单词。什么是循环? –
你有什么具体问题?您的实际结果与您的预期结果有何不同,以及您迄今为止尝试解决问题所做的工作? –
@DavidWallace'row + = word'修改循环内的'row'。一旦大小超过“宽度”,循环终止。 –