猿问

我如何在java中连续转义\ t?

我搜索了相同的问题。(如何在 Java 中转义字符串?)但我无法解决我的问题。

部分源代码 System.out.println("year : "+year+"\t"+"rate_year : "+rate_year+"\t"+"rate_month : "+rate_month);

但是当我打印它时。. . 年:10 rate_year:0.4 rate_month:0.03333333333333333

这首先打印 "\t" 为什么所有的 "\t" 都不适应???


慕桂英546537
浏览 167回答 1
1回答

慕容708150

"\t" 前进到下一个制表位。如果稍微更改文本描述,请注意对齐方式:System.out.println("year : "+year+"\t"+"rate_year : "+rate_year+"\t"+"rate_month : "+rate_month);System.out.println("year : "+year+"\t"+"ry : "+rate_year+"\t"+"rm : "+rate_month);输出:year : 10&nbsp; &nbsp;rate_year : 0.4 rate_month : 0.333333&nbsp; &nbsp;&nbsp;year : 10&nbsp; &nbsp;ry : 0.4&nbsp; &nbsp; &nbsp; &nbsp; rm : 0.333333我相信问题只是选项卡列下降的位置。另一方面,如果目标是列对齐,最好使用printf特定的列大小。参见,例如,这个关于用 \t 格式化的答案例子:for (int i = 0; i < 12; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("year : %3d rate_year : %6f rate_month : %-6f\n",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rate_year,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rate_month);}输出:year :&nbsp; &nbsp;8 rate_year : 0.400000 rate_month : 0.333333&nbsp;&nbsp;year :&nbsp; &nbsp;9 rate_year : 0.400000 rate_month : 0.333333&nbsp; &nbsp; &nbsp;&nbsp;year :&nbsp; 10 rate_year : 0.400000 rate_month : 0.333333&nbsp;&nbsp;year :&nbsp; 11 rate_year : 0.400000 rate_month : 0.333333&nbsp;&nbsp;
随时随地看视频慕课网APP

相关分类

Java
我要回答