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 6774605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:48:27+00:00 2026-05-26T15:48:27+00:00

I have a problem to draw a square ASCII with the size limit in

  • 0

I have a problem to draw a square ASCII with the size limit in 20 character (size of ‘square’), this is my code, I’ve tested and it works but when I put the number more than 20 it’s display an error, pls help, thanks.

class Main {
    public static void printSquare(int size) {
        int line = 1;

        while (line <= size) { // For each line of square
            int width = size; // width of square segment
            int i = 1; // display square segment

            while (i <= width && size <= 20) {
                System.out.print("*");
                i = i + 1;
            }

            System.out.println(); // Newline
            line = line + 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-26T15:48:27+00:00Added an answer on May 26, 2026 at 3:48 pm

    The problem is that you’ll never print the * if the size is greater than 20. A better way to restrict it to 20 would be to limit the size before the loop.

    public static void printSquare(int size) {
        if(size > 20) {
           size = 20;
        }
        int line = 1;
    

    and then edit

    while (i <= width && size <= 20) {
    

    to

    while (i <= width) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have code supposes to draw ring in random position within JPanel but for
I have a problem with my code after I click undo I can't draw
i have this problem when my Sprite rotation origin is fixed at top left
I don't know if this is the correct website, but you guys have been
I have problem to draw line on scaled and transformed image on canvas, well
I'm trying to draw curved arrows in a html canvas. I have no problem
I am trying to draw a red square over a JScrollPane. The code I
I have problem with displaying all the triangles. I draw a triangle using mouse
I have a problem with an action listener after I draw a line, basicly

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.