CheckerBox N by N

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mtarakji
    New Member
    • Oct 2007
    • 1

    #1

    CheckerBox N by N

    Hey guys, i got an assignment to write a java program that takes a command line argument (N) and creates a checkerBox like the one below if N is 5 :

    * * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    Pleas notice the small detail that first line starts with a "*" and the second with a space, and so on ...

    Till now, i have managed to write the folowing :

    public class checkerboard
    {
    public static void main(String[] args)
    {
    int n = Integer.parseIn t(args[0]);
    int i;
    for (i = 1; i<= n; i = i + 1)
    {
    System.out.prin t("* ");
    }
    System.out.prin tln();
    for (i = 1; i <= n; i = i + 1)
    {
    System.out.prin t(" *");
    }

    }
    }

    What this does is that it prints "*" N number of times, but only 2 lines, and not as many lines as N ... i am new to java and kinda stuck ... please, someone help ... ANYONE :D
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by mtarakji
    Hey guys, i got an assignment to write a java program that takes a command line argument (N) and creates a checkerBox like the one below if N is 5 :

    * * * * *
    * * * * *
    * * * * *
    * * * * *
    * * * * *
    Pleas notice the small detail that first line starts with a "*" and the second with a space, and so on ...

    Till now, i have managed to write the folowing :

    public class checkerboard
    {
    public static void main(String[] args)
    {
    int n = Integer.parseIn t(args[0]);
    int i;
    for (i = 1; i<= n; i = i + 1)
    {
    System.out.prin t("* ");
    }
    System.out.prin tln();
    for (i = 1; i <= n; i = i + 1)
    {
    System.out.prin t(" *");
    }

    }
    }

    What this does is that it prints "*" N number of times, but only 2 lines, and not as many lines as N ... i am new to java and kinda stuck ... please, someone help ... ANYONE :D
    Work yourself through this problem from the top to the bottom: you can print any
    'square' like this:

    [code=java]
    for (int y= 0; y < N; y++) { // rows
    for (int x= 0; x < N; x++) // columns
    System.out.prin tln(whatToPrint (y, x));
    System.out.prin tln();
    }
    [/code]

    The method 'whatToPrint(in t y, int x) determines what to print (sic) for a cell at
    position (y,x) and returns a String. I'm sure you can figure things out from here.

    kind regards,

    Jos

    Comment

    Working...