Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Method to print n x n box

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Method to print n x n box

    Stuck on this, need to create a method that prints n x n box:

    ****
    *  *
    *  *
    ****

    The spaces between the middle two stars is two.

    Here's my code:
        public static void starBox(int n)
        {
            for (int row = 1; row <= n; row++)
            {
                for (int col = 1; col <= row; col++)
                {
                    if (row == 1 || row == 4)
                    {
                        System.out.print("* ");
                    }
                    else
                    {
                        System.out.print("* ");
                }
                System.out.println();
            }
        }
    }
    }

    But I get this:
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    * 
    *

    Can anyone give me hints or tips as to what I'm doing wrong? Thanks!
    Last edited by HeroFlame; November 17th, 2011 at 12:02 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Method to print n x n box

    What is the value of row going to be the first time through your loop? So what will that do to the inner loop? What about the second time through the loop?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method to print n x n box

    Quote Originally Posted by KevinWorkman View Post
    What is the value of row going to be the first time through your loop? So what will that do to the inner loop? What about the second time through the loop?
    4 throughout.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Method to print n x n box

    Quote Originally Posted by HeroFlame View Post
    4 throughout.
    Really? Did you test that assumption? I suggest you run this program through a debugger, or at least add some print statements to figure out what's going on. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. HELP random generator with roll() and print() method
    By disc_dido in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 18th, 2011, 01:50 PM
  2. Method to print a diamond
    By snowMac in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 8th, 2011, 04:18 AM
  3. how can i print the out put !
    By Faha in forum Java IDEs
    Replies: 5
    Last Post: December 30th, 2010, 04:41 AM
  4. Print Goodbye
    By pslick29 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 13th, 2010, 02:31 PM
  5. Can someone please tell me why my code doesn't print out anything?
    By deeerek in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2010, 08:35 AM