Java: print diamond recursively

How would you print a diamond recursively using Java with the given dimensions?

Size 5 produces:

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

Code i still

 public static void dia(int statSize, int size,int count) { int statSizeLarge = (statSize*2)+1; // Params: // statSize == static size, never change this // size == variable size, change this // count == counter if(size==0) { System.out.println(); } else { // is the counter smaller then the size // if yes, increment and keep printing if(count<size){ System.out.print("*"); } // is greater then size? // if yes, move on, print // a few more stars if((count<=statSizeLarge)){ if(count<statSize+1 && (count>size)){ System.out.print(" "); }else if (count>size+1){ System.out.print("*"); } else {} dia(statSize,size,count+1); } // reset count, move to next element if(count>=statSizeLarge) { count = 0; System.out.println(); dia(statSize,size-1,count); } } // ends Else } 

OutPut:

 Enter commands: diamond 3 ****** ** **** * **** * **** ** **** * **** * **** 
+4
source share
3 answers

To create a larger diamond, take a smaller one and add two additional rows and columns. In the diagonal below, for clarity, I replace places with dots. In the second diamond, the newly added characters are shown in bold.

  *****. ***** <- extra row
 ****. **** ****.  .. ****
 *** ... *** *** .. ... ***
 ** ..... ** ** ... .. .. **
 * ....... * * .... .. ... *
 ......... -> ..... .. ....
 * ....... * * .... .. ... *
 ** ..... ** ** ... .. .. **
 *** ... *** *** .. ... ***
 ****. **** ****.  .. ****
               *****. ***** <- extra row
                    ^^
                    ||
                    extra columns

Your recursive function should print the first line, and then print a smaller diamond with two additional columns in the middle, then with the last line.

In pseudo code:

 void diamond(stars, spaces) { if (n == 0) { print(' ' * spaces) } else { print('*' * stars, ' ' * spaces, '*' * stars) diamond(stars - 1, spaces + 2) print('*' * stars, ' ' * spaces, '*' * stars) } } 

Since this is a training exercise, I will not give you the full Java source code - you can write it yourself. Here you can see how it works on the Internet in Python, so you can see that the algorithm works:

+6
source

Hint: find the template at the output. Try matching this pattern on recursive calls where the method does something, calls itself, and then does something else.

+1
source

Here is the Java program for printing diamond stars:

 class DiamondPattern { static public int ReadInteger() { try { String inpString = ""; InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); String s = reader.readLine(); return Integer.parseInt(s); } catch (Exception e) { e.printStackTrace(); } return -1; } public static void main(String[] args) { System.out.println("Program for displaying pattern of *."); System.out.print("Enter the maximum number of *: "); int n = ReadInteger(); System.out.println("\nHere is the Diamond of Stars\n"); for (int i = 1; i <= n; i++) { for (int j = 0; j < (n - i); j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); for (int k = 1; k < i; k++) System.out.print("*"); System.out.println(); } for (int i = n - 1; i >= 1; i--) { for (int j = 0; j < (n - i); j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); for (int k = 1; k < i; k++) System.out.print("*"); System.out.println(); } System.out.println(); } } 
0
source

Source: https://habr.com/ru/post/1341478/


All Articles