Printing a side triangle in java

I am trying to print a side triangle in java. If the user enters 5, the output should be:

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

If the user enters 6, the output should be:

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

I got it to work when the user enters 5, 3 or 1, but my code seems to work only for these three cases. I was wondering if anyone could help me get my code to work in more cases. Here he 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(); } } 
+1
java
source share
3 answers

This is much easier to solve with 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 ? There is no line!
  • n == 1 ? One line!
  • Otherwise? Two lines of sandwich line n - 2 ! (which are longer!)
+1
source share

Ok i will bite

So, the goal is to print a triangle of stars. Well, we need some kind of loop, perhaps with a different inner loop. And we know that it will be symmetrical because it is a triangle.

so I would start by printing the first 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 will need to deal with the second half of the triangle, which, since we have already reached the input value, means that we can simply return to it.

  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 subtracting two to the second, and if you don't, you will get the wrong answer. I’ll leave to understand why it’s up to you. If the pseudo-code entry is confused, please ask.

0
source share
 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(); } 

Explanation:

  • lines - input number (3,4,5,6,7, etc.).
  • get the middle row as double. That is, for odd numbers it will be x.5
  • the loop has as many rows as the input
  • on each line, print as many asterisks as there are in the asterisks variable
  • at each iteration, either increase the number of stars by 2 if the line is up to the middle, or decrease it if after. This means that if it is equal, nothing happens - i.e. The same line has the same number of stars. And it cannot be equal for odd numbers.
0
source share

All Articles