<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 /> {<br />  int inWeight, count;<br /><br />  for( inWeight=1; inWeight<=12; inWeight++)<br />  {<br />  while( count <= 12)<br />   {<br />   int sum = (sum + inWeight);<br />   }<br />     count = count + 1;<br /><br />  do<br />    {<br />     inWeight = ConsoleInput.readInt("Enter the weight for the month");<br />    <br />      if (inWeight < 0)<br /><br />        System.out.println("The weight must be a postive number");<br />     } <br />     while( inWeight < 0);<br />  }<br /> return sum;<br /> }</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;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.