i attempting have diamond print out based on user input using loops. i've gotten print out right side of diamond can't print out left side. i've tried reversing code no avail. i'm having trouble figuring out logic have code print out entire diamond. in advance!
the expected output if num=2 (without lines, of course):
* *$* *
here's code far:
//print out diamond shape based on user input (int i=num; i>0; --i){ system.out.print("*"); (int n=i; n<num; ++n){ system.out.print("$*"); }//ending bracket of nested loop system.out.println(); }//ending bracket of loop //print out diamond shape based on user input (int i=0; i<num; ++i){ system.out.print("*"); (int n=i; n<num; ++n){ system.out.print("$*"); }//ending bracket of nested loop system.out.println(); }//ending bracket of loop
you need add loop (one top half , 1 bottom half) in add enough spaces on each line in order center diamond:
for (int i=num; i>0; --i){ //insert spaces in order center diamond (int n=0; n<i; ++n){ system.out.print(" "); } system.out.print("*"); (int n=i; n<num; ++n){ system.out.print("$*"); }//ending bracket of nested loop system.out.println(); }//ending bracket of loop //print out diamond shape based on user input (int i=0; i<=num; ++i){ //<= print last asterisk //insert spaces in order center diamond (int n=0; n<i; ++n){ system.out.print(" "); } system.out.print("*"); (int n=i; n<num; ++n){ system.out.print("$*"); }//ending bracket of nested loop system.out.println(); }//ending bracket of loop
i'm not sure logic of correlation between num
, shape of diamond in example. code prints desired diamond num = 1
not num = 2
. in code num
determines maximum width of diamond (the number of $
signs). num = 2
prints:
* *$* *$*$* *$* *
if want
* *$* *
for num=1
, on, can replace every occurence of num
num-1
in code above. alternatively, add line
num--;
before code , leave rest is.