Exercise CheckerBoard (nested-loop) ~ Programming is Fun
 Programming is Fun!

www.anandsingh.net
Subscribe For Free Updates!

We'll not spam mate! We promise.

Friday, September 12, 2014

Exercise CheckerBoard (nested-loop)

Write a program called CheckerBoard that displays the following n×n (n=7) checkerboard pattern using two nested for-loops.
# # # # # # #
 # # # # # # #
# # # # # # #
 # # # # # # #
# # # # # # #
 # # # # # # #
# # # # # # #
Your program should use only three output statements, one EACH of the followings:
System.out.print("# ");   // print # and a space, without newline
System.out.print(" ");    // print a space, without newline
System.out.println();     // print a newline
Hints:
public class CheckerBoard {   // saved as "CheckerBoard.java"
   public static void main (String[] args) {
      int size = 7;    // size of the board

      for (int row = 1; ......; ......) {
         // Use modulus 2 to find alternate lines
         if ((row % 2) == 0) {   // row 2, 4, 6, 8
            ......
         }
         for (int col = 1; ......; ......) {
            ......
         }
         ......
      }
   }
}


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment