提问者:小点点

如何创建一个使数字右侧对齐的java程序?


我的目的是一个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条件,但我做不到。


共3个答案

匿名用户

这里有一个不同的方式来考虑它使用字符串连接。

您可以先生成最后一行,以确定每一行必须具有的最大宽度。然后,对于每一行,您从当前行号向后计数到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

匿名用户

您的代码很好地完成了工作。你的一个问题是,一旦你得到的数字超过1位数,它就不能正常工作:在你的例子中,是10和11。

解决这个问题实际上有点棘手——如果我输入“120392”怎么办?

你有几个选择。每个选项都更加神奇,但也需要更多的代码。

  1. 限制您的输入。例如,禁止超过 99 的输入,并假设所有数字的 # 位等于允许的最大数字(即 2 位数字)。
  2. 计算输入中的 # 位,然后假设所有数字都具有计算出的 # 位数。
  3. 只要做对,应用的间距随着数字的增加而增加。

如果你选择第一个或第二个选项,你会得到这样的结果:

                                 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

请注意,这比您的示例有更多的空格(之间有2个空格,例如每个43而不是1。

那么,怎么做呢?对于#1和#2,您需要System. out.printf("-");-这意味着:打印一个数字,但如果需要少于2个字符,则使用带空格的左键盘。

对于#1,您对“2”进行硬编码(并硬编码99个限制;或者,对3个空格进行硬编码,并编码999个限制)。对于#2,你得到你的输入,然后计算出有多少数字。类似于<code>String.valueOf(limit).length()将执行此操作,然后使用此长度构造格式字符串。

对于#3,您在< code>System.out.print(" ")中跟踪没有打印的数字;循环,这样您仍然可以计算出需要留出多长的空白:如果您不打印10,则需要3个空格。如果你不印500张,你需要4张。如果你不打印5,你需要2。

对于#2和#3:<code>printf(“%4s”,“”)打印4个空格。这是您用来要求java打印X个空格的方法。对于解决方案#2和#3,您将需要此功能。

这听起来像是第一周的作业。我认为#1是最合适的。

匿名用户

为了使2位数字正确对齐,其中一种方法是——在“coll”的for循环之后,将每个数字转换为String,然后反转它,然后打印它。如果你不将其转换为String,那么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();
}