943,731 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2043
  • Java RSS
Sep 24th, 2009
0

need help drawing a square with nested loops

Expand Post »
ok now what i want out of this program is to be able to ask the user how large the height and width of the drawing should be. (i seem to have this down ok or at least i think i do)
i also want to be able to have it so that the program should accept only odd numbers, when the user asks for an even number, ask again. (i cannot seem to figure out this portion too well)
also i want draw a square of the desired size utilizing the “*” character for the boundaries of the box, a “@” for the very center of the box, and “\” for the remaining parts(i cant figure out where to place the portion of the loop for the @)
this is what i have so far for the code.....its messy and all but ah well
the only import i want in this is the java.swing so no scanner or array or anything
i dont even want the if...else in there but i dont know how to do it with just the for loop
Java Syntax (Toggle Plain Text)
  1. import javax.swing.JOptionPane;
  2.  
  3. public class ex7 {
  4. public static void main(String[] args) {
  5. int shapeWidth = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the width of your shape")));
  6. int shapeheight = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the height of your shape")));
  7.  
  8. for (int i = 1; i <= shapeheight; i++) {
  9.  
  10. for (int j = 1; j <= shapeWidth; j++) {
  11. if (i == 1 || i == shapeheight)
  12. System.out.print('*');
  13. else if (j == 1 || j == shapeWidth)
  14. System.out.print('*');
  15. else
  16. System.out.print('/');
  17. }
  18. System.out.println();
  19.  
  20. }
  21. }
  22. }
Similar Threads
Reputation Points: 37
Solved Threads: 0
Newbie Poster
Zero-Method is offline Offline
6 posts
since Sep 2009
Sep 24th, 2009
1

Re: need help drawing a square with nested loops

You have two separate problems. Approach them separately and independently:
  1. Get good input from user.
  2. Draw rectangle based on that input.

Part 1 - You must ask the user for input. You must check that input to see if it meets your requirements. If not, you should tell the user what he did wrong and give him a chance to enter it again. Hence the code where you ask for input must be inside some sort of loop, either a for-loop or a while loop. How many times will the user enter bad data? You have no idea, so the loop should be a while loop, not a for-loop.

Part 2 - Once you have good data, you need to draw the rectangle. There's more than one way of doing it. You can break it up into five stages.
  1. Print the top line.
  2. Print the lines that are not the top line, but are above the center line.
  3. Print the center line.
  4. Print the lines that are not the bottom line, but are below the center line.
  5. Print the bottom line.

Depending on how many lines there are, you may not need to do all of them. If the height is less than 5, you will not do all five steps. Do the steps that you need to do in order.

Within steps 2, 3, and 4, you'll be displaying different characters, so split it into sub-steps.

Breaking the larger task into these smaller tasks will make things more manageable.
Last edited by VernonDozier; Sep 24th, 2009 at 6:20 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: ByteBuffer Question.
Next Thread in Java Forum Timeline: A function that returns a BigInteger





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC