473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.parseInt(args[0]);
int i;
for (i = 1; i<= n; i = i + 1)
{
System.out.print("* ");
}
System.out.println();
for (i = 1; i <= n; i = i + 1)
{
System.out.print(" *");
}

}
}

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
Oct 24 '07 #1
1 1080
JosAH
11,448 Expert 8TB
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.parseInt(args[0]);
int i;
for (i = 1; i<= n; i = i + 1)
{
System.out.print("* ");
}
System.out.println();
for (i = 1; i <= n; i = i + 1)
{
System.out.print(" *");
}

}
}

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:

Expand|Select|Wrap|Line Numbers
  1. for (int y= 0; y < N; y++) { // rows
  2.    for (int x= 0; x < N; x++) // columns
  3.       System.out.println(whatToPrint(y, x));
  4.    System.out.println();
  5. }
  6.  
The method 'whatToPrint(int 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
Oct 24 '07 #2

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.