Proble with a while loop

I don't have a compiler but what I would like to know is whether the program below will give me the correct output.
For example: if I enter numbers 2, 3 ,5 ,7, -1.
                 max should be 7
                 min should be 2
                 average should be 17/4
                 -1 is for program termination.

If I'm wrong please help me .
with kind regards
*******************************************************

import javabook.*;

class average;
{
 public static void main(String[]args)
  {
    int sum =0,num,max,min,index=1;
     
      num = inputbox.getdouble("Enter a number >=0");
       
     
        while( num >=0)
            {
                sum += num;
                    index++;
      num = inputbox.getdouble("Enter a number >=0");    
            }
             
           if (num >max)
               max =num;
            if (num < min)
                min = num;

   outpubox.print ("The maximum is :" + max);
   outpubox.print ("The minimum is :" + min);
   outpubox.print ("The average of all numbers is:" +sum/index);

}
}
sourceotherAsked:
Who is Participating?
 
umangjoshiConnect With a Mentor Commented:
//fllowing is the correct one

public static void main(String[]args)
{
   int sum =0,num,max,min,index=1;
   num = inputbox.getdouble("Enter a number >=0");
   max = min = num;    
   while( num >=0)
   {
       if (num > max)
          max =num;
       if (num < min)
          min = num;
       
       sum += num;
       index++;
       num = inputbox.getdouble("Enter a number >=0");    
   }        

  outpubox.print ("The maximum is :" + max);
  outpubox.print ("The minimum is :" + min);
  outpubox.print ("The average of all numbers is:" +sum/index);
}

0
 
objectsCommented:
should be:

public static void main(String[]args)
{
  int sum =0,num,max=Integer.MIN_VALUE,min=Integer.MAX_VALUE,index=0;
  num = inputbox.getdouble("Enter a number >=0");
  max = min = num;    
  while( num >=0)
  {
      if (num > max)
         max =num;
      if (num < min)
         min = num;
     
      sum += num;
      index++;
      num = inputbox.getdouble("Enter a number >=0");    
  }        

if (index>0)
{
 outpubox.print ("The maximum is :" + max);
 outpubox.print ("The minimum is :" + min);
 outpubox.print ("The average of all numbers is:" +sum/index);
}
}

0
 
CleanupPingCommented:
sourceother:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
girionisCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to umangjoshi

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
All Courses

From novice to tech pro — start learning today.