John Fraser Computer Contest Club

Forums

Post Reply
Forum Home > Java > A way to draw ASCII rhombus with given odd int as height

DOM
Member
Posts: 11

import java.io.*;

public class rhombus

{

    public static void main (String[] args)throws IOException

    {

        BufferedReader in = new BufferedReader(new FileReader("rhombus.txt"));

        PrintWriter out = new PrintWriter(new FileWriter("rhombusOut.txt"));

 

        String inputString;

 

        while((inputString=in.readLine())!=null){

            int height= Integer.parseInt(inputString);

            int mid= (height-1)/2;

            char map[][] = new char[height][height];

            for(int row=0;row<height;row++){

                for(int col=0;col<height;col++){

                    map[row][col]='.';}

 

            }//fill map

            for(int i=0;i<=mid;i++){

                for(int a=-i;a<=i;a++){

                map[i][mid+a]='#';

                }

            }//fill top half rhombus

            for(int i=(mid+1);i<=(height-1);i++){

                for(int a=-((height-1)-i);a<=(height-1-i);a++){

                map[i][mid+a]='#';

                }

            }//fill second half rhombus

            for(int row=0;row<height;row++){

                for(int col=0;col<height;col++){

                    out.print(map[row][col]);

                }

                out.println();

             }//output

        }//end of 1 case

        out.close();

    } // main method

} // rhombus class


November 1, 2010 at 3:55 PM Flag Quote & Reply

You must login to post.