design - Using nested loops to create a box with stars in java -



design - Using nested loops to create a box with stars in java -

i new java , taking intro course.

i have been able figure out bulk of question stuck on lastly step.

the end result supposed using 4 or less system.out.print or system.out.println statements:

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

and have created this

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

this code. there guys can see help?

public class starpatterns { public static void main(string[] args) { int col; int row; (row = 6; row >= 0 ; row--) { if (row >= 0) system.out.print("*"); (col = row; col < 6; col++) { system.out.print(" "); } (col = row; col > 0; col--) { system.out.print("*"); } system.out.println(); } } }

public class stars { public static void main(string[] args) { int row = 7; int col = 7; int count; (int i=0;i<row;i++) { count = i; system.out.print("*"); (int c=1;c<col;c++) { if (count == 0 || count == row-1) { system.out.print("*"); } else { system.out.print(" "); count--; } } system.out.println(""); } }

4 system.out prints:

update:

3 system.out prints:

public static void main(string[] args) { int row = 7; int col = 7; int count; (int i=0;i<row;i++) { count = i; //system.out.print("*"); (int c=0;c<col;c++) { if (count == 0 || count == row-1 || c == 0) { system.out.print("*"); } else { system.out.print(" "); count--; } } system.out.println(""); } }

probably not optimal (memory usage) maybe logic:

using 2 system.out prints:

public static void main(string[] args) { int row = 7; int col = 7; int count; string strnewline = "*" + system.lineseparator(); string str = "*"; string strtoprint; (int i=0;i<row;i++) { count = i; (int c=0;c<col;c++) { if (count == 0 || count == row-1 || c == 0) { if (c == row-1) { strtoprint = strnewline; } else { strtoprint = str; } system.out.print(strtoprint); } else { system.out.print(" "); count--; } } } }

java design

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -