我有一个关于打印字符串的问题。该程序应该执行以下工作:获取一个字符串作为参数并识别单词并将它们打印在对齐的三列中:示例:
the quick brown fox jumped over the lazy dog
输出应该是:
the quick brown
fox jumped over
the lazy dog
我的解决方案是
private void printColumn(String s){
StringTokenizer toker = new StringTokenizer(s);
while (toker.hasMoreTokens()){
String temp = "";
for (int i = 0; i < 3; i++){
temp +=toker.nextToken();
}
System.out.print(temp);
System.out.println();
}
}
但我的输出没有对齐
the quick brown
fox jumped over
the lazy dog
请问有什么建议吗?