Saturday, April 9, 2011

draw square on command prompt in java (fill)

public static String fill(char c,int size, int spaces_from_left_margin,int spaces_from_top , int spaces_from_bottom){

 StringBuffer sb = new StringBuffer();

  for(int i = 0 ; i < spaces_from_top;i++) sb.append("\n");
 
  for(int outer=0; outer < size ; outer++){

  for(int sflm=0 ; sflm 7lt spaces_from_left_margin ; sflm++){
  sb.append(" ");     
  }

  for(int inner=0; inner < size ; inner++){
  sb.append(c);
  }

  sb.append("\n");
  }

  for(int i = 0 ; i < spaces_from_bottom;i++) sb.append("\n");
  
  return sb.toString();
}
Below is the screenshot of the output when the method is called with fill('*',10,10,10,10).

No comments:

Post a Comment