Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Proble with a while loop

Posted on 2003-03-07
5
Medium Priority
?
210 Views
Last Modified: 2010-03-31
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);

}
}
0
Comment
Question by:sourceother
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 1

Accepted Solution

by:
umangjoshi earned 200 total points
ID: 8086804
//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
 
LVL 92

Expert Comment

by:objects
ID: 8086985
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
 

Expert Comment

by:CleanupPing
ID: 9058866
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
 
LVL 35

Expert Comment

by:girionis
ID: 9687045
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question