Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two fixes based on your current code.</p> <p><strong>Fix One</strong> - move variables from the class scope to the scope of the main method</p> <p>Currently your variables are sitting outside the main method but within the class. As the <code>main</code> method is static you can't reference them as they are variables of each instance of the PerfectNumbers class (and you aren't creating an instance of the PerfectNumbers class).</p> <p>Change:</p> <pre><code>int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4; public static void main(String[] args) { </code></pre> <p>to</p> <pre><code>public static void main(String[] args) { int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4; </code></pre> <p><strong>Fix Two</strong> - initialise your variables</p> <p>For primitive types (like <code>int</code>), Java needs to be guaranteed that your variables have been initialised. As you are currently initialising variables within conditional statements, Java can't be 100% guaranteed that they will be initialised.</p> <p>Change:</p> <pre><code>int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4; </code></pre> <p>to</p> <pre><code>int num = 1, divisor1 = 1, divisor2 = 1, divSum = 0, perfNum1 = 0, perfNum2 = 0, perfNum3 = 0, perfNum4 = 0; </code></pre> <p><strong>Note: These changes will make your code compile but it isn't yet correct. As this is homework, try and see if you can make an attempt to fix it yourself.</strong></p> <p><br><br></p> <p>A couple of cleanups to make things look a bit nicer</p> <p><strong>Cleanup One</strong> (as pointed out by @Sarma7)</p> <p>Remove this line:</p> <pre><code>import java.awt.*; </code></pre> <p>As you aren't using any methods/variables from this package.</p> <p><strong>Cleanup Two</strong></p> <p>It's good style to initialise one variable per line (if declaring without initialising then you can probably leave it on the same line).</p> <p>e.g.</p> <pre><code>int num = 1; int divisor1 = 1; int divisor2 = 1; int divSum = 0; int perfNum1 = 0; int perfNum2 = 0; int perfNum3 = 0; int perfNum4 = 0; </code></pre>
 

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