在我的计算结束时,我打印结果:
System.out.println("\nTree\t\tOdds of being by the sought author");
for (ParseTree pt : testTrees) {
conditionalProbs = reg.classify(pt.features());
System.out.printf("%s\t\t%f", pt.toString(), conditionalProbs[1]);
System.out.println();
}例如,这会产生以下结果:
Tree Odds of being by the sought author
K and Burstner 0.000000
how is babby formed answer 0.005170
Mary is in heat 0.999988
Prelim 1.000000仅仅把两个\t放在里面有点笨拙--列并不是真正对齐的。我更希望有这样的输出:
Tree Odds of being by the sought author
K and Burstner 0.000000
how is babby formed answer 0.005170
Mary is in heat 0.999988
Prelim 1.000000(注意:我在让SO文本编辑器完美地排列这些列时遇到了问题,但希望您能理解。)
有没有一种简单的方法可以做到这一点,或者我必须编写一个方法来尝试根据"Tree“列中字符串的长度来找出它?
发布于 2009-12-10 02:45:56
您正在查找字段长度。尝试使用以下命令:
printf ("%-32s %f\n", pt.toString(), conditionalProbs[1])-32告诉您字符串应该左对齐,但是字段长度为32个字符(根据您的喜好进行调整,我选择了32,因为它是8的倍数,这是终端上正常的制表位)。在标题上使用相同的代码,但使用%s而不是%f将使这一行变得更好。
发布于 2009-12-10 02:51:44
你需要的是令人惊叹而又免费的format()。
它的工作方式是让您在模板字符串中指定占位符;它生成模板和值的组合作为输出。
示例:
System.out.format("%-25s %9.7f%n", "K and Burstner", 0.055170);%s是字符串的占位符;%25s表示空白-将任何给定的字符串填充到25 characters.%-25s表示字段中的字符串左对齐,例如,string.%9.7f右侧的pad表示输出一个浮点数,该数字总共有9位,decimal.%n右侧的7位是“执行”行终止符所必需的,这是从System.out.format().转到System.out.println()时所缺少的
或者,您可以使用
String outputString = String.format("format-string", arg1, arg2...);创建输出字符串,然后使用
System.out.println(outputString);像以前一样打印它。
发布于 2009-12-10 02:46:29
怎么样
System.out.printf("%-30s %f\n", pt.toString(), conditionalProbs[1]);请参阅more information on the Formatter mini-language的文档。
https://stackoverflow.com/questions/1875936
复制相似问题