You are on page 1of 1

import java.util.

Scanner;

public class ClassSurveyJava {

public static void main(String[] args) {


Scanner input = new Scanner(System.in);

int oldestAge = -1;


String oldestName = "";
int youngestAge = Integer.MAX_VALUE;
String youngestName = "";

for (int i = 1; i <= 5; i++) {


System.out.print("Enter name for person " + i + ": ");
String name = input.nextLine();
System.out.print("Enter age for person " + i + ": ");
int age = input.nextInt();
input.nextLine(); // Consume the newline character

if (age > oldestAge) {


oldestAge = age;
oldestName = name;
}
if (age < youngestAge) {
youngestAge = age;
youngestName = name;
}
}

System.out.println("The oldest person is " + oldestName + " with age " +


oldestAge);
System.out.println("The youngest person is " + youngestName + " with age "
+ youngestAge);
}
}

You might also like