i'm a student who just started learning java in college. So in today's lesson my teacher gave me some assignments, including one that i must write a program, using the StdIn.Draw library, that when you type in a number N, it prints out an NxN chessboard with height and width N also. I decided to use an NxN array and set certain elements into a value (e.g true) and others into another value. After that, i used the library to draw the chessboard with respective to the array i just created. It looks like this:
class chessboard {
public static void main(String[] args) {
int N = StdIn.readInt();
boolean[][] hi = new boolean[N][N];
double r = 1 / N;
int x = 0;
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
if ((int)(i+j)%2 == 0)
hi[i][j] = true;
else {
hi[i][j] = false;
}
}
}
StdDraw.setXscale(0.0, 5.0);
StdDraw.setYscale(0.0, 5.0);
int i = 0;
int j = 0;
double a = 1.0;
double b = 1.0;
while ((a < N) && (i < N)){
while ((b < N) && (j < N)){
if (hi[i][j] != true){
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.filledSquare(a ,b, r);
}
else {
StdDraw.setPenColor(StdDraw.RED);
StdDraw.filledSquare(a ,b, r);
}
b = b + r;
j++;
}
b = 1.0;//reset b after the inner while loop
a = a + r;
i++;
}
}
}
When i test run it, it just prints out a white blank screen, nothing else. I couldn't find anything wrong with my algorithm, and i have tried to adjust the size of the initial a and b coordinate and the X and Y scale many times, but it just doesn't work. I tried to ask my friends about it, but they don't know either.
This is my screenshot of the problem:
Sorry if my English is bad, for i'm not a native speaker. And please if you could answer then keep it simple for me because i just started learning to code. Thank you.
Oh another thing. I'm studying based on this book: http://introcs.cs.princeton.edu/java/home/