Extend the summer camp program from a previous exercise so thatthe program also prints the oldest camper's name and age.
The following is a sample run (bold fonts represent userinputs):
Enter # of campers:3Enter the 3 campers' names one by one:Amy Bob CharlieEnter Amy's age:18Enter Bob's age:19Enter Charlie's age:17The campers:Amy (age 18)Bob (age 19)Charlie (age 17)The oldest camper is Bob (19 years old).
Main.Java New 1- import java.util.Scanner; 2 3 public class Main 4 - { 5 6 - 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter # of campers:"); int numCampers = input.nextInt(); } String[] names = new String[numCampers]; System.out.println("Enter the " + numCampers + campers' names one by one: "); //TODO 1: Use a loop to get the campers' names one by one int[] ages = new int[numCampers]; //TODO 2: Use a loop to get each camper's age. When prompting, Please mention each camper's name, such as "Enter Amy's age:" System.out.println("The campers:"); //TODO 3: Use a loop to print each camper's name and age //TODO4: Use a loop to find which camper is oldest //Display the oldest camper's name and age. PLEASE DO NOT CHANGE the code beloew. System.out.printf("The oldest camper is %s (%d years old).\n", names[indexOfMax], ages [indexOfMax]);
Extend the summer camp program from a previous exercise so that the program also prints the oldest camper's name and age
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am