java printf 需要帮助

发布于 2024-10-17 12:22:08 字数 492 浏览 2 评论 0原文

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

为什么第二行没有对齐?尽管单词“100”有足够的填充,但单词“Arcot”没有提供足够的填充。抱歉,此文本窗口应用其自己的格式,它没有显示我粘贴为输出的内容。您可能需要运行代码才能查看获得的输出。

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");
    }
}

It prints:

          Organization|               Number of users                          Arcot|                           100

Why is the 2nd row out of alignment? The word "Arcot" is not given enough padding, although the word "100" is. I'm sorry, this text window applies its own formatting, it is not showing what I have pasted as the output. You may need to run the code to see the output obtained.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木格 2024-10-24 12:22:08

试试这些。

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

Try these.

System.out.println(String.format("%30s|%30s","Organization","Number of users"));
    System.out.println(String.format("%30s|%30s","Arcot","100"));
伏妖词 2024-10-24 12:22:08
System.out.printf("%30s|%30s\n","Organization","Number of users");
System.out.printf("%30s|%30s\n","Arcot","100");

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

有关使用转义字符的详细信息。

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

You have to insert \n issue a newline character end of first parameter of printf.

More info about using escape character.

薯片软お妹 2024-10-24 12:22:08

这按预期工作:

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 是格式字符串中的首选表示法。

This works as expected:

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

results in

              Organization|               Number of users
                     Arcot|                           100

on my machine. You didn't add line feeds. %n is the preferred notation in format Strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文