我的字符串很长,一切都正确,但是我无法获得与教授相同的输出。他的输出更加统一。所以首先我尝试做一个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()方法。
答案 0 :(得分:2)
您可以使用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。
在这一点上,我会做实验。
对于浮点数(1.5),请执行%f。
System.out.printf("%3.1f\n", 1.5);
在此示例中,3是整个宽度,1是小数点后的位置。因此%10.2f将为您提供10个字符的宽度和小数点后2点。
答案 1 :(得分:1)
您可以使用String.format
统一输出。使用String.format
,您可以将文本输出长度定义为一种格式。
作为一个例子;
String firstValue = "Password = bank,Rabbither";
String secondValue = "Newline = 0";
String thirdValue = "Single = 2";
String str = String.format("%1$-20s | %2$-30s | %3$-30s",fristValue, secondValue, thirdValue);
对于1$-20s
:1$
表示第一个变量,-20s
表示字符串的固定长度。