Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how do i draw a shape with nested loops?

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how do i draw a shape with nested loops?

    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
    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();
     
        }
        }
    }


    really seem to be having a lot of trouble with this one


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how do i draw a shape with nested loops?

    public static void main(String[] args)
    {
         Scanner reader = new Scanner(System.in);
         int size = 0;
         while(size <= 0 || size % 2 == 0)
         {
              System.out.println("input an odd number: ");
              size = reader.nextLine();
         }
     
         for (int i = 0; i < size; i++)
         {
              for (int j = 0; j < size; j++)
              {
                   if (i == 0 || j == 0 || i = size-1 || j = size-1)
                   {
                        System.out.print("*");
                   }
                   else if (size/(i-1)==size/(j-1))
                   {
                        System.out.print("@");
                   }
                   else
                   {
                        System.out.print("/");
                   }
              }
              System.out.println("");
         }
    }

Similar Threads

  1. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 09:11 AM
  2. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 11:52 AM
  3. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 04:10 AM
  4. Replies: 11
    Last Post: April 29th, 2009, 04:12 AM
  5. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 04:25 AM