I need to modify it to prompt the user for how many ages they would like to enter.Run the user through a while loop prompting them to enter an age...
View the step-by-step solution to:

Question

 I need to modify it to prompt the user for how

many ages they would like to enter. Run the user through a while loop prompting them to enter an age for the number of times they provided. After the last age is entered, return the average age back to them. Verify that the age returned back to them displays a decimal point (double). The input of the ages should be integers - not doubles. No infinite loops.


import java.util.Scanner;

public class AverageAgeGurung {

 public static void main(String[] args) {

  Scanner in = new Scanner(System.in);

  System.out.print("This program will calculate the average age of "

    + "six people. " + "nn");

  

  System.out.print("Enter an age for person 1: ");

  int person1 = in.nextInt();

  

  System.out.print("Enter an age for person 2: ");

  int person2 = in.nextInt();

  

  System.out.print("Enter an age for person 3: ");

  int person3 = in.nextInt();

  

  System.out.print("Enter an age for person 4: ");

  int person4 = in.nextInt();

  

  System.out.print("Enter an age for person 5: ");

  int person5 = in.nextInt();

  

  System.out.print("Enter an age for person 6: ");

  int person6 = in.nextInt();

  

  double avg = (person1 + person2 + person3 + person4) / 6.0;

  System.out.print("nThe average age is: " + avg + " Years");

  

  

 }

}

Top Answer

View the full answer
Capture.PNG

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question