gpt4 book ai didi

java - 有没有一种方法可以格式化您的输出,使其更加统一?

转载 作者:行者123 更新时间:2023-12-04 12:26:12 30 4
gpt4 key购买 nike

我有一个很长的字符串,一切正常,但我无法获得与教授相同的输出。他的输出更均匀。所以首先我尝试做一个 system.out.println(),但是没有用。

这是我的代码:

System.out.println("Password = " + password + "\t\t" + "Newline = " + ctnline + "\t" + "Single = " + ctlength + "\t" + "Equal = " + ctequal + "\t" + "Lenght = " + ctlength2 + "\t" + "Upper = " + ctupper + "\t" + "Lower = " + ctlower + "\t" + "Special = " + (ctspec2));

这是输出:

Password = bank,Rabbither               Newline = 0     Single = 2      Equal = 0       Lenght = 1      Upper = 2       Lower = 0       Special = 1
Password = acrossShemoment, Newline = 0 Single = 3 Equal = 0 Lenght = 2 Upper = 9 Lower = 0 Special = 0
Password = her.theyOWN Newline = 0 Single = 6 Equal = 0 Lenght = 3 Upper = 9 Lower = 0 Special = 1
Password = ALICEeyes.said Newline = 0 Single = 0 Equal = 0 Lenght = 0 Upper = 0 Lower = 0 Special = 0
Password = thepackKing. Newline = 0 Single = 1 Equal = 0 Lenght = 3 Upper = 4 Lower = 0 Special = 1
Password = thethatKing. Newline = 0 Single = 1 Equal = 0 Lenght = 0 Upper = 2 Lower = 0 Special = 0
Password = AfterWhichdear! Newline = 0 Single = 1 Equal = 0 Lenght = 1 Upper = 3 Lower = 0 Special = 0
Password = "Silencethecrown Newline = 0 Single = 1 Equal = 1 Lenght = 6 Upper = 8 Lower = 0 Special = 0
Password = "Silencemuchme Newline = 0 Single = 6 Equal = 0 Lenght = 1 Upper = 6 Lower = 0 Special = 2
Password = King.nopeeped Newline = 0 Single = 5 Equal = 0 Lenght = 2 Upper = 9 Lower = 0 Special = 2
Password = eitherKing.Soon Newline = 0 Single = 0 Equal = 0 Lenght = 0 Upper = 0 Lower = 0 Special = 0

那没有用,老师有一个输出,其中“换行符”对齐在一起。

我需要的输出是这样的(老师怎么写的):

Password = bank,Rabbither       Newline = 0     Single = 2      Equal = 0       Lenght = 1      Upper = 2       Lower = 0       Special = 1
Password = acrossShemoment, Newline = 0 Single = 3 Equal = 0 Lenght = 2 Upper = 9 Lower = 0 Special = 0
Password = her.theyOWN Newline = 0 Single = 6 Equal = 0 Lenght = 3 Upper = 9 Lower = 0 Special = 1
Password = ALICEeyes.said Newline = 0 Single = 0 Equal = 0 Lenght = 0 Upper = 0 Lower = 0 Special = 0
Password = thepackKing. Newline = 0 Single = 1 Equal = 0 Lenght = 3 Upper = 4 Lower = 0 Special = 1
Password = thethatKing. Newline = 0 Single = 1 Equal = 0 Lenght = 0 Upper = 2 Lower = 0 Special = 0
Password = AfterWhichdear! Newline = 0 Single = 1 Equal = 0 Lenght = 1 Upper = 3 Lower = 0 Special = 0
Password = "Silencethecrown Newline = 0 Single = 1 Equal = 1 Lenght = 6 Upper = 8 Lower = 0 Special = 0
Password = "Silencemuchme Newline = 0 Single = 6 Equal = 0 Lenght = 1 Upper = 6 Lower = 0 Special = 2
Password = King.nopeeped Newline = 0 Single = 5 Equal = 0 Lenght = 2 Upper = 9 Lower = 0 Special = 2
Password = eitherKing.Soon Newline = 0 Single = 0 Equal = 0 Lenght = 0 Upper = 0 Lower = 0 Special = 0

我从来没有学过如何使用 printf,当我查找解决方案时,他们就是这样在网上展示它的。有没有其他方法可以在打印时正确格式化。我不确定我们是否可以使用 printf,因为老师从未教过它。

编辑:我可以使用 String.format() 方法。

最佳答案

您可以使用 System.out.printf()。它使用与 String.format() 相同的代码。

System.out.printf("Password = %20s Newline = %2d Single = %d Equal = %d Lenght = %d Upper = %d Lower = %d Special = %d\n",
password, ctnline, ctlength, ctequal, ctlength2, ctupper, ctlower, ctspec2);

格式字符串采用您喜欢的任何文本加上以 %s 开头的代码。您最常使用的两个是 %s——一个字符串——和 %d——一个十进制数。您可以在 google 上搜索 printf 格式字符串以进行更长时间的讨论。 %f 不太常见,但仍然很重要。我将在下面讨论。

%s 或 %d 可以采用可选长度。您会看到我做了 %20s——这意味着空格填充到 20 个字符,即使传入的字符串为空。你可以用数字做同样的事情。如果您不指定长度,它将根据需要使用多少。如果您指定的不够多,它将运行很长时间。

您可以用零填充数字,例如 %03d。例如,这会给你 004 而不是 4。

您可以使用 - 操作左对齐和右对齐。 %-20s,例如。

此时,我会进行实验。

对于 float (1.5),您执行 %f。

System.out.printf("%3.1f\n", 1.5);

在这个例子中,3是整个宽度,1是小数点后的位数。所以 %1​​0.2f 会给你 10 个字符宽和小数点后 2 点。

关于java - 有没有一种方法可以格式化您的输出,使其更加统一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58660400/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com