需要java printf帮助

时间:2011-02-16 07:10:36

标签: java printf

public class Format
{
    public static void main(String args[])
    {
        System.out.printf("%30s|%30s","Organization","Number of users");
        System.out.printf("%30s|%30s","Arcot","100");
    }
}

打印:

          Organization|               Number of users                          Arcot|                           100

为什么第二排不对齐? “Arcot”这个词没有给出足够的填充,尽管单词“100”是。对不起,这个文本窗口应用了自己的格式,它没有显示我粘贴的输出。您可能需要运行代码才能看到获得的输出。

3 个答案:

答案 0 :(得分:1)

试试这些。

System.out.println(String.format("%30s|%30s","Organization","Number of users"));
    System.out.println(String.format("%30s|%30s","Arcot","100"));

答案 1 :(得分:0)

System.out.printf("%30s|%30s\n","Organization","Number of users");
System.out.printf("%30s|%30s\n","Arcot","100");

您必须插入\n发出printf第一个参数的换行符结尾。

有关using escape character的更多信息。

答案 2 :(得分:0)

这可以按预期工作:

System.out.printf("%30s|%30s%n","Organization","Number of users");
System.out.printf("%30s|%30s%n","Arcot","100");

结果

              Organization|               Number of users
                     Arcot|                           100

在我的机器上。您没有添加换行符。 %n是字符串格式中的首选符号。