我如何创建一个 java 程序使数字右对齐?
How can I create a java program which makes numbers align-right side?
我的目的是 java 从用户那里获取号码的应用程序。启动 1 并将其写入控制台。每个新的行系列将增加 1,直到来自用户的数字标记。重要的是将每一行对齐到右侧。
Scanner in = new Scanner(System.in);
int numberOfLines = in.nextInt();
for (int rows = 1; rows <= numberOfLines; rows++) {
for (int i = numberOfLines - rows; i >= 1; i--) {
System.out.print(" ");
}
for (int col = rows; col >= 1; col--) {
System.out.printf(" %d",col);
}
System.out.println();
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
当达到两位数时,它不是右对齐文本。我试图在循环中使用 if 条件,但我做不到。
您的代码运行良好。你遇到的一个问题是,一旦你得到超过 1 位的数字,它就会停止正常工作:在你的例子中,10 和 11。
修复这个问题实际上有点棘手 - 如果我输入“120392”怎么办?
您有几个选择。每个选项都更神奇,但也需要更多代码。
- 限制您的输入。例如,不允许输入超过 99,并假设所有数字的位数等于最大允许数字(因此,2 位数字)。
- 计算输入中的位数,然后假设所有数字都具有计算出的位数。
- 只要正确,应用的间距会随着数字的增加而增加。
如果您选择第一个或第二个选项,您将得到如下内容:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
请注意,这比您的示例多了 spaces(中间有 2 spaces,例如每个 4
和 3
而不是 1.
那么,怎么样?对于 #1 和 #2,你需要 System.out.printf("%2d");
- 这意味着:打印一个数字,但如果它需要少于 2 个字符,则左填充 spaces.
对于#1,您对“2”进行硬编码(并硬编码 99 个限制;或者,硬编码 3 space 和 999 个限制)。对于#2,你得到你的输入,然后计算出它有多少位数。 String.valueOf(limit).length();
之类的东西会这样做,然后你使用这个长度构造格式字符串。
对于 #3,您在 System.out.print(" ");
循环中跟踪您 未打印 的数字,以便您仍然可以计算出空白 [=48] 的长度=] 你需要做的必须是:如果你不打印 10,你需要 3 spaces。如果您不打印 500,则需要 4。如果您不打印 5,则需要 2。
对于#2 和#3:printf("%4s", "");
打印 4 spaces。这就是你用来要求 java 打印 X 数量的 space 的内容。对于解决方案#2 和#3,您将需要它。
这听起来像是第一周的作业。我觉得#1最合适。
这是使用字符串连接的另一种思考方式。
您可以先生成最后一行,以确定每行必须达到的最大宽度。然后对于每一行,您从当前行号倒数到 1,这样您就知道数字部分的宽度。最后,您在前面加上使当前行与最后一行一样宽所需的空格数。 请注意,这是对字符串的一种非常低效的使用,但实际上我正在演示一种不同的算法。提高这种内存效率只会让它更难理解。另请注意,正确呈现的输出取决于您所在的环境 运行,以及它如何显示长字符串。一些系统会添加滚动条,而另一些系统可能会导致字符串换行。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Number of rows? ");
int numberOfLines = in.nextInt();
if (numberOfLines >= 1) {
int totalWidth = 0;
// add up the widths of the numbers themselves
for (int number = 1; number <= numberOfLines; number++) {
totalWidth = totalWidth + ("" + number).length();
}
// add in the spaces in-between the numbers
totalWidth = totalWidth + (numberOfLines - 1);
// now generate each row by counting backwards from the current row number
for (int rowNumber = 1; rowNumber<=numberOfLines; rowNumber++) {
String row = "";
for (int i=rowNumber; i>=2; i--) {
row = row + i + " ";
}
row = row + "1";
// prepend the spaces in front to make it as wide as the last row
int paddingLength = totalWidth - row.length();
for(int i=1; i<=paddingLength; i++) {
row = " " + row;
}
// output the row
System.out.println(row);
}
}
else {
System.out.println("Number of rows must be positive!");
}
}
25 行的示例输出:
Number of rows? 25
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
13 12 11 10 9 8 7 6 5 4 3 2 1
14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
为了使 2 位数字右对齐,其中一种方法是 - 在 'col' 的 for 循环之后将每个数字转换为字符串,然后反转它,然后打印它。如果您不将其转换为字符串,则 10、20、30 等数字将打印为 1、2、3 等
for (int rows = 1; rows <= numberOfLines; rows++) {
for (int i = numberOfLines - rows; i >= 1; i--) {
System.out.print(" ");
}
for (int col = rows; col >= 1; col--) {
if(col>9){
COL=Integer.toString(col);
for(int i=COL.length();i>=0;i--)
rev=rev+COL.charAt(i);
System.out.printf(" %d",rev);
}
else
System.out.printf("%d",col);
}
System.out.println();
}
我的目的是 java 从用户那里获取号码的应用程序。启动 1 并将其写入控制台。每个新的行系列将增加 1,直到来自用户的数字标记。重要的是将每一行对齐到右侧。
Scanner in = new Scanner(System.in);
int numberOfLines = in.nextInt();
for (int rows = 1; rows <= numberOfLines; rows++) {
for (int i = numberOfLines - rows; i >= 1; i--) {
System.out.print(" ");
}
for (int col = rows; col >= 1; col--) {
System.out.printf(" %d",col);
}
System.out.println();
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
当达到两位数时,它不是右对齐文本。我试图在循环中使用 if 条件,但我做不到。
您的代码运行良好。你遇到的一个问题是,一旦你得到超过 1 位的数字,它就会停止正常工作:在你的例子中,10 和 11。
修复这个问题实际上有点棘手 - 如果我输入“120392”怎么办?
您有几个选择。每个选项都更神奇,但也需要更多代码。
- 限制您的输入。例如,不允许输入超过 99,并假设所有数字的位数等于最大允许数字(因此,2 位数字)。
- 计算输入中的位数,然后假设所有数字都具有计算出的位数。
- 只要正确,应用的间距会随着数字的增加而增加。
如果您选择第一个或第二个选项,您将得到如下内容:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
请注意,这比您的示例多了 spaces(中间有 2 spaces,例如每个 4
和 3
而不是 1.
那么,怎么样?对于 #1 和 #2,你需要 System.out.printf("%2d");
- 这意味着:打印一个数字,但如果它需要少于 2 个字符,则左填充 spaces.
对于#1,您对“2”进行硬编码(并硬编码 99 个限制;或者,硬编码 3 space 和 999 个限制)。对于#2,你得到你的输入,然后计算出它有多少位数。 String.valueOf(limit).length();
之类的东西会这样做,然后你使用这个长度构造格式字符串。
对于 #3,您在 System.out.print(" ");
循环中跟踪您 未打印 的数字,以便您仍然可以计算出空白 [=48] 的长度=] 你需要做的必须是:如果你不打印 10,你需要 3 spaces。如果您不打印 500,则需要 4。如果您不打印 5,则需要 2。
对于#2 和#3:printf("%4s", "");
打印 4 spaces。这就是你用来要求 java 打印 X 数量的 space 的内容。对于解决方案#2 和#3,您将需要它。
这听起来像是第一周的作业。我觉得#1最合适。
这是使用字符串连接的另一种思考方式。
您可以先生成最后一行,以确定每行必须达到的最大宽度。然后对于每一行,您从当前行号倒数到 1,这样您就知道数字部分的宽度。最后,您在前面加上使当前行与最后一行一样宽所需的空格数。 请注意,这是对字符串的一种非常低效的使用,但实际上我正在演示一种不同的算法。提高这种内存效率只会让它更难理解。另请注意,正确呈现的输出取决于您所在的环境 运行,以及它如何显示长字符串。一些系统会添加滚动条,而另一些系统可能会导致字符串换行。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Number of rows? ");
int numberOfLines = in.nextInt();
if (numberOfLines >= 1) {
int totalWidth = 0;
// add up the widths of the numbers themselves
for (int number = 1; number <= numberOfLines; number++) {
totalWidth = totalWidth + ("" + number).length();
}
// add in the spaces in-between the numbers
totalWidth = totalWidth + (numberOfLines - 1);
// now generate each row by counting backwards from the current row number
for (int rowNumber = 1; rowNumber<=numberOfLines; rowNumber++) {
String row = "";
for (int i=rowNumber; i>=2; i--) {
row = row + i + " ";
}
row = row + "1";
// prepend the spaces in front to make it as wide as the last row
int paddingLength = totalWidth - row.length();
for(int i=1; i<=paddingLength; i++) {
row = " " + row;
}
// output the row
System.out.println(row);
}
}
else {
System.out.println("Number of rows must be positive!");
}
}
25 行的示例输出:
Number of rows? 25
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
11 10 9 8 7 6 5 4 3 2 1
12 11 10 9 8 7 6 5 4 3 2 1
13 12 11 10 9 8 7 6 5 4 3 2 1
14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
为了使 2 位数字右对齐,其中一种方法是 - 在 'col' 的 for 循环之后将每个数字转换为字符串,然后反转它,然后打印它。如果您不将其转换为字符串,则 10、20、30 等数字将打印为 1、2、3 等
for (int rows = 1; rows <= numberOfLines; rows++) {
for (int i = numberOfLines - rows; i >= 1; i--) {
System.out.print(" ");
}
for (int col = rows; col >= 1; col--) {
if(col>9){
COL=Integer.toString(col);
for(int i=COL.length();i>=0;i--)
rev=rev+COL.charAt(i);
System.out.printf(" %d",rev);
}
else
System.out.printf("%d",col);
}
System.out.println();
}