简体   繁体   English

在Java中打印侧身三角形

[英]Printing a sideways triangle in java

I'm trying to print a sideways triangle in java. 我正在尝试在Java中打印一个横向三角形。 If the user enters 5, the output should be: 如果用户输入5,则输出应为:

      *
      ***
      *****
      ***
      *

If the user enters 6, the output should be: 如果用户输入6,则输出应为:

      *
      ***
      *****
      *****
      ***
      *

I've gotten it to work for the case when the user enters 5, 3, or 1 but my code seems to work for those three cases only. 我已经使它适用于用户输入5、3或1的情况,但是我的代码似乎仅适用于这三种情况。 I was wondering if anyone could help me get my code working for more cases. 我想知道是否有人可以帮助我使我的代码在更多情况下正常工作。 Here it is: 这里是:

public void printArrow( int n )
{ int asterisks = 1;
   for ( int i = 0; i <= n/2; i++ )
   {
       for ( int j = i; j < asterisks; j++ )
       {

         System.out.print( "*" );

        }
        asterisks += 3;
        System.out.println();
    }


    asterisks = asterisks / 2 - 2;
    for ( int i = 0;  i < n/2; i++ )
    {
        for ( int k = i; k < asterisks; k++ )
        {
            System.out.print( "*" );

        }
        if ( i == 1 )
        {
            System.out.print( "*" );
        }

        asterisks -= 2;
        System.out.println();
    }
}

It's much easier to solve this using recursion: 使用递归解决这个问题要容易得多:

static String triangle(int n, String s) {
    return
        n == 0 ? "" :
        n == 1 ? s  :
        s
          + 
            triangle(n - 2, "**" + s)
          +
        s
    ;
}

public static void main(String args[]) {
    System.out.println(triangle(6, "*\n"));
}

The structure of the triangle is self-evident: 三角形的结构不言而喻:

  • n == 0 ? n == 0 No line! 不行!
  • n == 1 ? n == 1 One line! 一条线!
  • Otherwise? 除此以外? Two lines sandwiching n - 2 lines! 两行夹着n - 2行! (which are longer!) (更长!)

Alright Will I'll bite 好吧,我会咬

So the goal is to print out a triangle of stars. 因此,目标是打印出三角形的星星。 Well we are going to need a loop of some kind, probably with another internal loop. 好吧,我们将需要某种类型的循环,可能需要另一个内部循环。 And we know that it's going to be symmetric as it's a triangle. 而且我们知道它将是对称的,因为它是一个三角形。

so I'd start with printing the fist half: 所以我将从打印拳头开始:

function triangle( input )
    i <- 1
    while i < input do
        for j from 1 to i do
            print "*"
        end for
        i <- i + 2
        print "\n"
    end while

After that we'd need to deal with the second half of the triangle which, because we have already walked i up to the input value means we can just walk it back down. 之后,我们需要处理三角形的下半部分,因为我们已经使i上升到输入值,这意味着我们可以将其回退。

    if i > input then i <- i - 2

    while i > 0 do
        for j from 1 to i do
            print "*"
        end for
        i <- i - 2
        print "\n"
    end while
end function triangle

the little trick in it that almost caught me is the subtraction of two before the second while, if you don't do this you'll get the wrong answer. 几乎让我着迷的一个小窍门是在第二秒之前减去两个,如果您不这样做,则会得到错误的答案。 I'll leave figuring out why up to you. 我将找出原因由您决定。 If there is confusing in the pseudocode notation please ask. 如果伪代码表示法令人困惑,请询问。

double middle = ((double) lines) / 2;
int asterisks = 1;
for (int i = 1; i <= lines; i ++){
    for (int k = 0; k < asterisks; k ++) {
        System.out.print("*");
    }

    if (i < middle) {
        asterisks += 2;
    } else if (i > middle){
        asterisks -= 2;
    }
    System.out.println();
}

Explaining: 说明:

  • lines is the input number (3,4,5,6,7, etc) lines是输入数字(3、4、5、6、7等)
  • get the middle row as a double. 将中间行作为双打。 Ie for odd numbers it will be x.5 即奇数将是x.5
  • the loop is for as many lines as the input is 循环与输入一样多
  • on each line print as many asterisk as there are in the asterisks variable 每行上打印的星号与asterisks变量中的一样多
  • on each iteration either increase the number of asterisk by 2, if the line is before the middle, or decrease it, if after. 在每次迭代中,如果该行在中间之前,则将星号的数量增加2,如果在该行之后,则将其减少。 This means that if it is equal, nothing happens - ie the same row has the same number of asterisk. 这意味着如果相等,则什么也不会发生-即同一行具有相同数量的星号。 And it can't be equal for odd numbers. 而且它不能等于奇数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM