有没有一种方法可以格式化您的输出,使其更加统一?
Is there a way to format your output so that it comes out more uniformly?
我的字符串很长,一切正常,但我无法获得与教授相同的输出。他的输出更均匀。所以首先我尝试做一个 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
那没有用,老师有一个 "newline" 对齐在一起的输出。
我需要这样的输出(老师怎么写的):
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 for printf format strings 进行更长时间的讨论。 %f 不太常见,但仍然很重要。我将在下面讨论。
%s 或 %d 可以采用可选长度。您会看到我做了 %20s——这意味着 space 填充到 20 个字符,即使传入的字符串为空。你可以用数字做同样的事情。如果您不指定长度,它将根据需要使用多少。如果您指定的不够多,它将 运行 长。
您可以将数字填零,例如 %03d。例如,这会给你 004 而不是 4。
您可以使用 - 操作左对齐和右对齐。 %-20s,例如。
此时,我会进行实验。
对于浮点数 (1.5),您执行 %f。
System.out.printf("%3.1f\n", 1.5);
在这个例子中,3是整个宽度,1是小数点后的位数。所以 %10.2f 会给你 10 个字符宽和小数点后 2 点。
您可以使用 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
表示您的字符串固定长度。
我的字符串很长,一切正常,但我无法获得与教授相同的输出。他的输出更均匀。所以首先我尝试做一个 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
那没有用,老师有一个 "newline" 对齐在一起的输出。
我需要这样的输出(老师怎么写的):
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 for printf format strings 进行更长时间的讨论。 %f 不太常见,但仍然很重要。我将在下面讨论。
%s 或 %d 可以采用可选长度。您会看到我做了 %20s——这意味着 space 填充到 20 个字符,即使传入的字符串为空。你可以用数字做同样的事情。如果您不指定长度,它将根据需要使用多少。如果您指定的不够多,它将 运行 长。
您可以将数字填零,例如 %03d。例如,这会给你 004 而不是 4。
您可以使用 - 操作左对齐和右对齐。 %-20s,例如。
此时,我会进行实验。
对于浮点数 (1.5),您执行 %f。
System.out.printf("%3.1f\n", 1.5);
在这个例子中,3是整个宽度,1是小数点后的位数。所以 %10.2f 会给你 10 个字符宽和小数点后 2 点。
您可以使用 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
表示您的字符串固定长度。