Q BgQuestion:

      
Pupil
Karma Points: 55
Respect (99%):
posted by  Ruger_Mk2 on 11/3/2009 4:23:42 PM  |  status: Closed  |  Earned Karma: 55

PLZ HELP ASAP!!!! JAVA

Course Textbook Chapter Problem Needs by
N/A N/A N/A N/A 11/3/2009 at 11:00:00 PM
Question Details:
Write a program that reads any number of integers from the keyboard, then
calculates and prints the average.  Assume the last value read is the sentinel (exit
condition) 9999.  The sentinel, 9999, should not be included in the average.  
Name the .java file Lab7_Ex2.
 
Example input/output:
 
Enter any number of integers (enter 9999 when done):
8 [enter]
11 [enter]
7 [enter]
9 [enter]
2 [enter]
9999 [enter]
 
The average is 7.4
 
Note: The value “7.4” is not an integer.
Bonus Point Alert! Earn +15 additional karma points for helping this platinum member.

AAnswers:

Answer Question Ask for clarification
Oracle
Karma Points: 18,830
posted by Marth on 11/3/2009 6:29:54 PM  |  status: Live
Asker's Rating: Lifesaver   
Response Details:
public static void main(String[] args)
{      
      // open user input stream
      Scanner kb = new Scanner(System.in);

      // input numbers while the input is not 9999
      int input;

      // store total and count of numbers inputted
      double total = 0.0;
      int count = 0;
     
      System.out.println("Enter any number of integers (enter 9999 when done): ");

      while((input=kb.nextInt())!=9999)
      {
            total += input;
            count++;
      }      

      // calculate average
      double average = total / count;

      // output
      System.out.println("The average is "+average);
}


Thank you for rating my response. Feel free to PM me if you have further questions.

Java Genius

"Lois Lane is falling, accelerating at an initial rate of 32ft per second per second. Superman swoops down to save her by reaching out two arms of steel. Ms. Lane, who is now traveling at approximately 120 miles per hour, hits them, and is immediately sliced into three equal pieces." - Sheldon
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »