Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6791547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:51:35+00:00 2026-05-26T17:51:35+00:00

hi i would like to draw a square with * as an outline and

  • 0

hi i would like to draw a square with * as an outline and dot in the middle with size range 4 -20.

*****
*...*
*...*
*...*
*****

i’m having a trouble to make the height equal with the length.
this is my code, could you please help.. thanks

class Main
{
  public static void printSquare( int size )
  {
    if ( size >= 20 && size >= 4) 
    { size = 4; }

    int squareLenght = size;
    int i = 1; 
    int p = 1;

    if ( p <= size )
    {
       int t = 1;

       while ( t <= squareLenght  )
       {
        System.out.print( "*" );
        t = t + 1;
       }
    }
       System.out.println();           //  Newline
       i = i + 1;


    while ( i <= squareLenght )
    {

       int d = 1; 
       int s = 1;


       if ( s < squareLenght  );{
       System.out.print( "*" ); 
       s = s + 1;
       }

       while ( d < size-1  )
       {
        System.out.print( "." );
        d = d + 1;

       }


       System.out.println( "*" );
        i = i + 1;


    }

    if ( p <= size )
    {
       int t = 1;

       while ( t <= squareLenght  )
       {
        System.out.print( "*" );
        t = t + 1;
       }
    }

    System.out.println();           
       i = i + 1;

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:51:35+00:00Added an answer on May 26, 2026 at 5:51 pm

    You’re printing 1 too many lines in the body (in between the borders) because of this line:

    while ( i <= squareLenght )

    It should be while ( i < squareLenght) OR while ( i <= squareLenght - 1 ) since there should be 2 borders and size - 2 rows in the middle that make up the height.

    Also, this doesn’t make any sense:

    if ( size >= 20 && size >= 4) 
    { size = 4; }
    

    Perhaps for the range you want:

    if (size > 20) size = 20;
    else if (size < 4) size = 4;
    

    Anyway your code’s a bit of a mess with unnecessary if statements and just the whole structure of things. for loops would also be a good idea where you have while loops with an initialization, a condition and a modifier/increment. I’m not going to change it for you because it sounds like homework.

    Anyway if you wanted to neaten it up, think about the actual structure and pattern of what you’re doing, because it’s fairly simple:

    • Print size amount of asterisks ('*') and a new line (top border)
    • Loop size - 2 times: (if size is 5, there are 3 lines in between the borders, ie. size - 2)
      • Print '*'
      • Print '.' size - 2 times
      • Print '*'
      • Print a new line
    • Print size amount of asterisks ('*') and a new line (bottom border)

    EDIT: I know you’ve already marked the question as answered, but just wanted to suggest you have a function like printRepeatedly(char c, int count) to reduce the amount of loops that you’re repeating (it’ll just be a for loop and a print statement).

    I just rewrote your code and I only required 11 lines of code in printSquare, plus the 2 in printRepeatedly). If you’re going to repeat code you should nearly always use functions/methods instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to draw a small square, which is 4x4 pixels. I would
I would like to draw lines (of arbitrary position and length) onto a surface
I would like to draw text on a canvas using the Canvas.drawText call for
I would like to draw some shapes on a JPanel by overriding paintComponent .
I would like to draw a chart in OpenGL similar to the donut graph
I would like to draw a line or bar graph using HTML5 canvas. I
I would like to draw a box using SAS/GRAPH with a color gradient (say
in vb.net i would like to draw a regular line on a form. is
We have an array of points through which we would like to draw a
In C# i have a picturebox. i would like to draw 4 colors. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.