Java Eclipse homework. I need help coding this|

Challenge 7A - My Family

package: chall7A
class: MyFamily

Write a program that will do the following:

a. Ask the user to input the names and ages of three friends or family members. (Create appropriate variables for storage of these.)

b. Calculate the oldest, youngest and average age of the three people.

c. Display the names, ages, and calculations in the console window in an appropriate format.

Respuesta :

import java.util.Scanner;

import java.util.Arrays;

public class MyFamily {

   public static void main(String[] args) {

       float avg = 0;

       int smallest = 0;

       int largest = 0;

       Scanner scan = new Scanner(System.in);

       System.out.println("Enter the name of a friend or family member.");

       String name1 = scan.nextLine();

       System.out.println("Enter "+name1+"'s age");

       int name1Age = scan.nextInt();

       System.out.println("Enter the name of a friend or family member.");

       String name2 = scan.next();

       System.out.println("Enter "+name2+"'s age");

       int name2Age = scan.nextInt();

       System.out.println("Enter the name of a friend or family member.");

       String name3 = scan.next();

       System.out.println("Enter "+name3+"'s age");

       int name3Age = scan.nextInt();

       int[] ages = {name1Age, name2Age, name3Age};

       Arrays.sort(ages);

       System.out.println("The oldest is " + ages[2] + " years old");

       System.out.println("The youngest is " + ages[0] + " years old");

       System.out.println("The average is (("+name1Age + "+" + name2Age + "+" + name3Age+") / 3) = " + ((name1Age + name2Age + name3Age) / 3) + " years old");

       System.out.println(name1+", "+name1Age + " years old");

       System.out.println(name2+", "+name2Age + " years old");

       System.out.println(name3+", "+name3Age + " years old");

       

   }

   

}

I hope this helps!