有一种简单的方法可以在Java中将两列输出到控制台吗? [英] Is there an easy way to output two columns to the console in Java?

查看:114
本文介绍了有一种简单的方法可以在Java中将两列输出到控制台吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,有一种简单的方法可以用Java输出两列到控制台吗?

As the title says, is there an easy way to output two columns to the console in Java?

我知道 \\ \\ t ,但是在使用printf时我没有找到基于特定列的空间方法。

I'm aware of \t, but I haven't found a way to space based on a specific column when using printf.

推荐答案

使用宽度和精度说明符,设置为相同的值。这将填充太短的字符串,并截断太长的字符串。 ' - '标志将左对齐列中的值。

Use the width and precision specifiers, set to the same value. This will pad strings that are too short, and truncate strings that are too long. The '-' flag will left-justify the values in the columns.

System.out.printf("%-30.30s  %-30.30s%n", v1, v2);

这篇关于有一种简单的方法可以在Java中将两列输出到控制台吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆