Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're printing 1 too many lines in the body (in between the borders) because of this line:</p> <p><code>while ( i &lt;= squareLenght )</code></p> <p>It should be <code>while ( i &lt; squareLenght)</code> <strong>OR</strong> <code>while ( i &lt;= squareLenght - 1 )</code> since there should be 2 borders and <code>size - 2</code> rows in the middle that make up the height.</p> <p>Also, this doesn't make any sense:</p> <pre><code>if ( size &gt;= 20 &amp;&amp; size &gt;= 4) { size = 4; } </code></pre> <p>Perhaps for the range you want:</p> <pre><code>if (size &gt; 20) size = 20; else if (size &lt; 4) size = 4; </code></pre> <p>Anyway your code's a bit of a mess with unnecessary <code>if</code> statements and just the whole structure of things. <code>for</code> loops would also be a good idea where you have <code>while</code> loops with an initialization, a condition and a modifier/increment. I'm not going to change it for you because it sounds like homework. </p> <p>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:</p> <ul> <li>Print <code>size</code> amount of asterisks (<code>'*'</code>) and a new line (top border)</li> <li>Loop <code>size - 2</code> times: (if <code>size</code> is 5, there are 3 lines in between the borders, ie. <code>size - 2</code>) <ul> <li>Print <code>'*'</code></li> <li>Print <code>'.'</code> <code>size - 2</code> times</li> <li>Print <code>'*'</code></li> <li>Print a new line</li> </ul></li> <li>Print <code>size</code> amount of asterisks (<code>'*'</code>) and a new line (bottom border)</li> </ul> <p><strong>EDIT</strong>: I know you've already marked the question as answered, but just wanted to suggest you have a function like <code>printRepeatedly(char c, int count)</code> to reduce the amount of loops that you're repeating (it'll just be a <code>for</code> loop and a print statement).</p> <p>I just rewrote your code and I only required 11 lines of code in <code>printSquare</code>, plus the 2 in <code>printRepeatedly</code>). If you're going to repeat code you should nearly always use functions/methods instead.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload