How to make a diamond shape with asterisks in Java

1 Answer

0 votes
package javaapplication1;

import static java.lang.Math.abs;
import static java.lang.Math.floor;

public class JavaApplication1 {

    public static void main(String[] args) {
        
        int size = 11;
        for (int i = 0; i < size; i++)
        {
            for (int j = 0; j < size; j++)
            {
                System.out.print(floor(abs(j - size/2)) + 
                                 floor(abs(i - size/2)) <= floor(size/2) ? "*" : " ");
            }
            System.out.println();
        }
    }
}

/*
run:

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

*/

 



answered Jul 6, 2016 by avibootz
...