如何在java中打印数字三角形

时间:2014-02-05 03:41:41

标签: java

我需要生成如图所示的三角形:

    1
   22
  333
 4444
55555

我的代码是:

int i, j;
for(i = 1; i <= 5; i++)
{
    for(j = 1; j <= i; j++)  
    {          
        System.out.print(i); 
    }      
    System.out.print("\n");        
}

以相反的方式制作三角形

1
22
333
4444
55555

我需要对代码做些什么才能使其面向正确的方式?

8 个答案:

答案 0 :(得分:2)

你需要3个for循环:

  1. 要重复和打印的实际数字的上级循环
  2. 打印空间的第一个内层
  3. 重复打印数字的第二个内部级别
  4. 在上层循环结束时打印新行
  5. 代码:

    public void printReversedTriangle(int num)
    {
        for(int i=0; i<=num; i++)
        {
            for(int j=num-i; j>0; j--)
            {
                System.out.print(" ");
            }
            for(int z=0; z<i; z++)
            {
                System.out.print(i);
            }
            System.out.println();
        }
    }
    

    输出:

         1
        22
       333
      4444
     55555
    666666
    

答案 1 :(得分:0)

我在AP CS课上遇到过这个问题。我想你可能已经开始学习如何编程所以我会做什么而不给你答案。

使用循环删除每次迭代的空格数。第一次通过你想要打印四个空格然后打印1次(可能在一个单独的循环中完成)。 下一次通过一个较小的空间,但再打印一次。

答案 2 :(得分:0)

您需要打印一些空格。您需要的空格数与您要打印的数字(i)之间存在关系。您可以使用以下方法打印X个空格:

for (int k = 0; k < numSpaces; k++)
{
    System.out.print(" ");
}

所以在你的代码中:

int i, j;
for(i = 1; i <= 5; i++)
{
    // Determine number of spaces needed
    // print spaces
    for(j = 1; j <= i; j++)  
    {          
        System.out.print(i); 
    }      
    System.out.print("\n");        
}

答案 3 :(得分:0)

使用此代码,

int i, j,z;
            boolean repeat = false;
            for (i = 1; i <= 5; i++) {
                repeat = true;
                for (j = 1; j <= i; j++) {
                    if(repeat){
                        z = i;
                        repeat = false;
                        while(z<5){
                        System.out.print(" ");
                        z++;
                        }
                    }
                    System.out.print(i);
                }

                {
                    System.out.print("\n");
                }
            }

答案 4 :(得分:0)

您可以使用:

int i, j;
int size = 5;
for (i = 1; i <= size; i++) {
    if (i < size) System.out.printf("%"+(size-i)+"s", " ");
    for (j = 1; j <= i; j++) {
        System.out.print(i);
    }
    System.out.print("\n");
}

这一行:     if (i < size) System.out.printf("%"+(size-i)+"s", " ");

打算打印左侧空格。

它使用旧的printf,其中包含固定大小的字符串,如5个字符:%5s

在此处试试:http://ideone.com/jAQk67

答案 5 :(得分:0)

当我在控制台上格式化时,我有时会遇到麻烦...... ...我通常会将这个问题提取到一个单独的方法中......

关于如何创建数字和间距的所有内容已经发布,所以这可能有点过分了^^

/**
* creates a String of the inputted number with leading spaces
* @param number the number to be formatted
* @param length the length of the returned string
* @return a String of the number with the size length 
*/
static String formatNumber(int number, int length){

    String numberFormatted = ""+number; //start with the number
    do{
        numberFormatted = " "+numberFormatted; //add spaces in front of
    }while(numberFormatted.length()<length);   //until it reaches desired length

    return formattedNumber;
}

该示例可以轻松修改,甚至可用于字符串或其他任何^^

答案 6 :(得分:0)

使用三个循环,它将产生您所需的输出:

    for (int i=1;i<6 ;i++ )
    {
        for(int j=5;j>i;j--)
        {
            System.out.print(" ");
        }
        for(int k=0;k<i;k++)
        {
            System.out.print(i);
        }
        System.out.print("\n");
    }

答案 7 :(得分:0)

您的代码不会产生相反的结果,因为相反的意思是您有空格但在右侧。输出的右侧是空的,让你觉得你有相反的结果。您需要包含空格才能形成所需的形状。

试试这个:

public class Test{
    public static void main (String [] args){
        for(int line = 1; line <= 5; line++){
            //i decreases with every loop since number of spaces
            //is decreasing
            for(int i =-1*line +5; i>=1; i--){
                System.out.print(" ");
            }
            //j increases with every loop since number of numbers
            //is decreasing
            for(int j = 1; j <= line; j++){
                System.out.print(line);
            }
            //End of loop, start a new line
            System.out.println();
        }
    }
}

通过从行数开始,您正确地解决了问题。接下来,您必须在行数(第一个用于循环)和for循环内部之间建立关系。如果你想这样做,请记住这个公式:

变化率*线+ X =线上元素数

您可以通过查看每行后元素数量的变化来计算变化率。例如,在第一行您有4个空格,在第二行您有3个空格。你做3 - 4 = -1,换句话说,你移动到每一行,空格数减少1.现在选一行,让我们说第二行。通过使用公式

-1(变化率)* 2(线)+ X = 3(你选择的线上有多少个空格)。

你得到的X = 5,你可以在你的代码中使用你的公式,你可以在for循环的第4行看到。

  

for(int i = -1 * line +5 ; i&gt; = 1; i - )

你对每一行的数字量做同样的事情,但由于变化率为1,即每行的数量增加1,因为元素的数量等于行,X将为0号。

  

for(int j = 1; j&lt; = line ; j ++){