little java help with loops -
hi doing practice problems , trying print diagonal line example below. have writen program see below , dont understand doing wrong. m java beginner , cant see how find error.
example:
* * * * *
code:
class diagonal{ public static void main(string args[]) { int row, col; for(row = 1; row < 6; row++) { for(col = 1; col <= row; col++) { if(col==row){ system.out.print("*"); } else{ system.out.print(""); } system.out.println(); } } } }
i trying learn loops because confuse me. practice print similar diagonal line time right left. cant without getting right :( believe pretty similar? above reasining this: long column # same row number print line or otherwise leave blank....what's wrong how did it?
thank you!
you never print space character. print empty string. replace
system.out.print("");
with
system.out.print(" ");
also, write newline after each column rather writing 1 after each row.
Comments
Post a Comment