| | |
need help drawing a square with nested loops
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 6
Reputation:
Solved Threads: 0
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
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)
import javax.swing.JOptionPane; public class ex7 { public static void main(String[] args) { int shapeWidth = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the width of your shape"))); int shapeheight = (Integer.parseInt(JOptionPane.showInputDialog("Enter the value for the height of your shape"))); for (int i = 1; i <= shapeheight; i++) { for (int j = 1; j <= shapeWidth; j++) { if (i == 1 || i == shapeheight) System.out.print('*'); else if (j == 1 || j == shapeWidth) System.out.print('*'); else System.out.print('/'); } System.out.println(); } } }
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
You have two separate problems. Approach them separately and independently:
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.
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.
- Get good input from user.
- 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.
- Print the top line.
- Print the lines that are not the top line, but are above the center line.
- Print the center line.
- Print the lines that are not the bottom line, but are below the center line.
- 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.
![]() |
Similar Threads
- Program Help Using For Nested Loops (C++)
- Help with Nested Loops, C (C)
- converting Nested loops into recursion (C++)
- Nested For Loops (C++)
Other Threads in the Java Forum
- Previous Thread: ByteBuffer Question.
- Next Thread: A function that returns a BigInteger
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation banking binary bluetooth character chat class classes client code component constructor crashcourse database design developmenthelp draw eclipse error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide if_statement image input integer interface j2me java javaarraylist javadoc javaee javamicroeditionuseofmotionsensor javaprojects jetbrains jmf jni jpanel julia linux list loop map method methods mobile multithreading netbeans newbie number object oracle pearl print printing problem program programming project recursion remove scanner screen server set size sms socket software sort splash sql stop string swing test textfield thread threads time tree validation windows






