need help drawing a square with nested loops

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 6
Reputation: Zero-Method is an unknown quantity at this point 
Solved Threads: 0
Zero-Method Zero-Method is offline Offline
Newbie Poster

need help drawing a square with nested loops

 
0
  #1
Sep 24th, 2009
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,837
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: need help drawing a square with nested loops

 
1
  #2
Sep 24th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC