java - trying to make a diamond but cant understand how to finish it? -
hey guy iv been trying same exercises on own lately, , thought making diamond out of astricks cool , got stuck on last part. im getting , cant figure out how flip triangle, or make complete diamond
* *** ***** public static string drawdiamond(int n) { string results = ""; int cols = 1; int spaces = n / 2; while (cols <= n) { results += drawchars(" ", spaces) + drawchars("*", cols) + "\n"; /*-------------------------------------------------------------------------------- while(cols>=n){ results += drawchars(" ", spaces) + drawchars("*", cols)+"\n";<--- test code. cols-=2; spaces++; } */-------------------------------------------------------------------------------- cols += 2; spaces--; } return results; }
just extending logic , assuming n=5, after 3rd iteration cols value become 7 , hence coming out of loop. when cols reached n value, should reverse process increment spaces , decrement cols.
make sure use other variable loop condition, otherwise go infinite loop
Comments
Post a Comment