Results 1 to 3 of 3
  1. #1
    AZjava_newbie is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Help with printing hollow squares?

    Hi guys,
    My assignment is to write a program to take a user input from 0-64 for n, and print a square with sides of length n. The corners are to be plus signs (+) while the edges straight lines (- or |) like so

    Enter length between 0 and 64 (-1 to exit): <4>
    length entered: 4
    +--+
    | |
    | |
    +--+

    Also, I need the program to loop continuously, asking the user for new inputs and printing the new square until the input is -1, at which point the program exits, totaling the number of squares printed as well as the average length the user entered. Lastly, I must use a while, do-while, and for statement at some point in the code. My problem is that after the first square is printed, the following inputs don't print a square properly. I would greatly appreciate some pointers as to where I went wrong. Here is the code so far. Thank you!


    Java Code:
    import java.util.Scanner;
    
    public class PrintSquares {
    
    	
    public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	int n;
    	int row =1;
    	int column;
    	int squares = 0;
    	double total = 0;
    	
    	System.out.println("Enter length between 0 and 64 (-1 to exit): ");
    	n = input.nextInt();
    	
    	if (n>64 || n<-1) {
    		System.out.println(n+" is invalid\nLength must be between 0 and 64 inclusive, or enter -1 to exit.");
    		System.exit(1);
    	}
    
    else do	{
    	squares++;
    	total = total + n;
    while ( row <= n )
    {
    for (column=1; column <= n; column++ )
    {
    if (row == 1 && column == n || row == n && column == 1 || row == n && column == n || row == 1 && column == 1)
    	System.out.print("+");
    else if ( row == 1 )
    	System.out.print( "-" );
    else if ( row == n )
    	System.out.print( "-" );
    else if ( column == 1 )
    	System.out.print( "|" );
    else if ( column == n )
    	System.out.print( "|" );
    else
    	System.out.print( " " );
    }
    System.out.println();
    row++;
    }System.out.println("Enter length between 0 and 64 (-1 to exit): ");
    n = input.nextInt();
    if (n>64 || n<-1) {
    	System.out.println(n+" is invalid\nLength must be between 0 and 64 inclusive, or enter -1 to exit.");
    	System.exit(1);
    }
    }while (n != -1);
    
    if (n==-1) {
    	System.out.println(squares+" squares printed. Average length: "+(total/squares));
    	System.exit(0);
    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: Help with printing hollow squares?

    The best advice I can give you is: use methods.

    You could have a method that just prints the top/bottom line. (methodA)
    have a method that prints a single line for the sides. (methodB)

    Then you could have another method that:
    calls methodA
    calls methodB n-2 times
    class methodA

    Then the main method would loop asking user input as long as it isn't -1.

    The advantage to this is that you have broken the code into smaller chunks and you can work on each chunk one at a time. If there is a problem then you know it exists within that small chunk than somewhere in ALL your code if you have it crammed together in the main method.

  3. #3
    AZjava_newbie is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help with printing hollow squares?

    Thanks so much for your reply. We haven't actually covered methods yet in this class, but I did a little bit of extra reading and was able to figure the rest out. I really appreciate the help!

Similar Threads

  1. Hollow square n x n logic issue - help!
    By xcaldk74 in forum New To Java
    Replies: 2
    Last Post: 04-04-2012, 08:48 PM
  2. help with magic squares
    By grandmastertheory in forum New To Java
    Replies: 2
    Last Post: 01-18-2012, 12:06 AM
  3. Magic squares help
    By mjpam in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 02:24 PM
  4. help with perfect squares
    By AmplifiedKid in forum New To Java
    Replies: 1
    Last Post: 09-19-2009, 07:44 PM
  5. Perfect Squares
    By divyachaparala in forum New To Java
    Replies: 4
    Last Post: 02-05-2008, 09:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •