Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

<p>I can\'t figure out why this isn\'t working. Basically i need to input a posi

ID: 3628782 • Letter: #

Question

<p>I can't figure out why this isn't working. Basically i need to input a positive weight for each month and loop the input until it is postive, then output the average weight of the 12 months. I think something is wrong with the variable sum but can't figure it out.</p>
<p>private static int totalWeight()<br />&#160;{<br />&#160; int inWeight, count;<br /><br />&#160; for( inWeight=1; inWeight&lt;=12; inWeight++)<br />&#160; {<br />&#160; while( count &lt;= 12)<br />&#160;&#160; {<br />&#160;&#160; int sum = (sum + inWeight);<br />&#160;&#160; }<br />&#160;&#160;&#160;&#160; count = count + 1;<br /><br />&#160; do<br />&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160; inWeight = ConsoleInput.readInt("Enter the weight for the month");<br />&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160; if (inWeight &lt; 0)<br /><br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.out.println("The weight must be a postive number");<br />&#160;&#160;&#160;&#160; } <br />&#160;&#160;&#160;&#160; while( inWeight &lt; 0);<br />&#160; }<br />&#160;return sum;<br />&#160;}</p>

Explanation / Answer

// works perfectly now...give a try.

private static int totalWeight()
{
int inWeight, count;
int sum=0;
for( count=1; count<=12; count++)
{
do
    {
     inWeight = ConsoleInput.readInt("Enter the weight for the month");
   
      if (inWeight < 0)

        System.out.println("The weight must be a postive number");
    } while( inWeight < 0);

   sum = sum +inWeight;
}


return sum;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote