Results 1 to 3 of 3
- 10-19-2012, 04:53 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
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); } } }
- 10-19-2012, 05:07 AM #2
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.
- 10-22-2012, 05:14 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Hollow square n x n logic issue - help!
By xcaldk74 in forum New To JavaReplies: 2Last Post: 04-04-2012, 08:48 PM -
help with magic squares
By grandmastertheory in forum New To JavaReplies: 2Last Post: 01-18-2012, 12:06 AM -
Magic squares help
By mjpam in forum New To JavaReplies: 3Last Post: 06-30-2010, 02:24 PM -
help with perfect squares
By AmplifiedKid in forum New To JavaReplies: 1Last Post: 09-19-2009, 07:44 PM -
Perfect Squares
By divyachaparala in forum New To JavaReplies: 4Last Post: 02-05-2008, 09:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks