java - Printing a shape based on inputs from the user -
i trying print shape based on input; shape "x". inputs must positive odd ints, , arbitrary brush character. have code completed user input, need code prints shape. here have far:
public class testprogram { public static void main(string[] args) { int height = 5;//any positive odd int 5 not work correctly. not sure going on. char brush = '*'; (int row = 0; row < height/2; row++) { (int = row; > 0; i--) { system.out.print(" "); } system.out.print(brush); (int = (height/2); >= 2*row; i--) { system.out.print(" "); } system.out.print(brush); system.out.print("\n"); } (int row = 1; row < (height/2)+1; row++ ) { system.out.print(" "); } system.out.print(brush); system.out.print("\n"); (int row = (height/2)-1; row >= 0; row--) { (int = row; > 0; i--) { system.out.print(" "); } system.out.print(brush); (int = (height/2); >= 2*row; i--) { system.out.print(" "); } system.out.print(brush); system.out.print("\n"); } (int row = 1; row < (height/2)+1; row++ ) { system.out.print(" "); } } }
i start routine repeat char
n
times. like
private static string repeat(char ch, int count) { stringbuilder sb = new stringbuilder(); (int = 0; < count; i++) { sb.append(ch); } return sb.tostring(); }
then, prefer build string
stringbuilder
, repeat
routine. like
int height = 5; char brush = '*'; char space = ' '; int half = height / 2; stringbuilder sb = new stringbuilder(); (int row = 0; row < half; row++) { int cols = height - ((row + 1) / 2); sb.append(repeat(space, row)).append(brush); sb.append(repeat(space, cols)).append(brush); sb.append(system.lineseparator()); } sb.append(repeat(space, half)).append(brush); sb.append(system.lineseparator()); (int row = half - 1; row >= 0; row--) { int cols = height - ((row + 1) / 2); sb.append(repeat(space, row)).append(brush); sb.append(repeat(space, cols)).append(brush); sb.append(system.lineseparator()); } system.out.println(sb.tostring());
since haven't learned buffered io yet, expressed as
int height = 5; char brush = '*'; char space = ' '; int half = height / 2; (int row = 0; row < half; row++) { int cols = height - ((row + 1) * 2); system.out.print(repeat(space, row)); system.out.print(brush); system.out.print(repeat(space, cols)); system.out.println(brush); } system.out.print(repeat(space, height / 2)); system.out.println(brush); (int row = (height / 2) - 1; row >= 0; row--) { int cols = height - ((row + 1) * 2); system.out.print(repeat(space, row)); system.out.print(brush); system.out.print(repeat(space, cols)); system.out.println(brush); }
and if don't know how create method,
int height = 5; char brush = '*'; char space = ' '; int half = height / 2; (int row = 0; row < half; row++) { int cols = height - ((row + 1) * 2); (int t = 0; t < row; t++) { system.out.print(space); } system.out.print(brush); (int t = 0; t < cols; t++) { system.out.print(space); } system.out.println(brush); } (int t = 0; t < height / 2; t++) { system.out.print(space); } system.out.println(brush); (int row = (height / 2) - 1; row >= 0; row--) { int cols = height - ((row + 1) * 2); (int t = 0; t < row; t++) { system.out.print(space); } system.out.print(brush); (int t = 0; t < cols; t++) { system.out.print(space); } system.out.println(brush); }
Comments
Post a Comment