little java help....

Posted by jona on Stack Overflow See other posts from Stack Overflow or by jona
Published on 2011-03-02T23:19:46Z Indexed on 2011/03/02 23:24 UTC
Read the original article Hit count: 240

Filed under:

Hi I am doing some practice problems and trying to print a diagonal line like the example below. I have writen the program you see below and I honestly dont understand what I am doing wrong. I m a java beginner and I cant see how to find the error.

Example ( If you only see a straight line of stars...then imagine it diagonally....from top left to bottom right)

*
  *
    *
      *
        *

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 am trying to learn for loops because they really confuse me. Another practice is to print a similar diagonal line but this time from right to left. I cant do that without getting this right however :( I believe they will be pretty similar? Above my reasining is this: As long as the column # is the same as the row number the print the line or otherwise leave a blank....what's wrong with how i did it?

THANK YOU!

© Stack Overflow or respective owner

Related posts about java