Results 1 to 3 of 3
  1. #1
    onlyone is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Output a square of "*"'s depending on the width and height a user specifies...?

    Java Code:
    import java.util.*;
    
    public class Grid
    {
        public static void main (String[]args)
        {
            Scanner kybd = new Scanner(System.in);
            
            System.out.print("Enter a width: ");
            int width = kybd.nextInt();
            System.out.print("Enter a height: ");
            int height = kybd.nextInt();
            
            for (height = height; height > 0; height--)
            {
                for (width = width; width > 0; width--)
                {
                    System.out.print("*");
                    System.out.println();
                }
                
                System.out.print("*");
            }
            
        }
    }
    I want a box widthxheight of "*"'s.
    Ie. if the user enters 3 as width and 4 as height:

    ***
    ***
    ***
    ***

    Should be displayed. I've played around with it a lot but that example now gives me:

    *
    *
    *
    *
    ***

    Thank you for any help

  2. #2
    flo28rin is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Output a square of "*"'s depending on the width and height a user specifies...?

    Java Code:
    import java.util.*;
    
     
    public class one
    {
        public static void main (String[]args)
        {
            Scanner kybd = new Scanner(System.in);
             
            System.out.print("Enter a width: ");
            int width = kybd.nextInt();
            System.out.print("Enter a height: ");
            int height = kybd.nextInt();
             
            for (int i = height; i > 0; i--){
                for (int j = width; j > 0; j--)
                	System.out.print("*");
                System.out.println();
            }
             
        }
    }
    You decremented your height and width - you need new variables in those for-s to got from your desired height to 0.
    Last edited by flo28rin; 10-26-2011 at 10:03 PM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Output a square of "*"'s depending on the width and height a user specifies...?

    Hurray! Spoonfeeding!

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 07-16-2011, 01:01 AM
  2. output "left click"/enter command?
    By Arsenic in forum New To Java
    Replies: 2
    Last Post: 06-04-2009, 06:01 AM
  3. Replies: 2
    Last Post: 04-28-2009, 05:52 AM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. "Displayed tab width" problem...
    By Petike in forum Eclipse
    Replies: 0
    Last Post: 03-17-2008, 10:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •