import java.util.Scanner;
import java.util.ArrayList;
public class Part5 {
public static void main (String[] args) {
// Name Array List
ArrayList <String> name = new ArrayList <String>();
name.add("Mason");
name.add("Bubba");
name.add("Jill");
name.add("Pat");
name.add("Jenny");
name.add("Howard");
name.add("Buster");
// Age Array List
ArrayList <Integer> age = new ArrayList <Integer>();
age.add(25);
age.add(44);
age.add(16);
age.add(18);
age.add(21);
age.add(44);
age.add(8);
Scanner user = new Scanner(System.in);
// if I place a loop here, all I get is an empty console. I don't know how to fix it
System.out.println("Enter a letter to exit the program. Enter an integer to find a list of names " +
"(1) equal to, (2) older than, or (3)younger than a certain age: ");
int i = user.nextInt();
// Originally I used an if-else statement, but again, the program wouldn't work.
switch (i) {
case 1: {
System.out.println("You have chosen number 1. Enter in age for comparison: ");
int e = user.nextInt();
if (e == age.get(e)) {
System.out.println(name.get(e) + ":" + age.get(e));
}
else {
System.out.println("There are no names associated with that age.");
}
}
break;
case 2 : {
System.out.println("You have chosen number 2. Enter in age for comarison: ");
int e = user.nextInt();
if (e <= age.get(e)) {
System.out.println(name.get(e) + ":" + age.get(e));
}
}
break;
case 3: {
System.out.println("You have chosen number 1. Enter in age for comarison: ");
int e = user.nextInt();
if (e >= age.get(e)) {
System.out.println(name.get(e) + ":" + age.get(e));
}
}
break;
default: {
System.out.println("Invalid Entry.");
}
break;
}
user.close();
}
}
// Given the following people’s names and their ages, you are asked to write a Java program to finish the following tasks. Write a program to display names and ages based on user questions according to the following requirements. First, you must build the arrays - one for names and one for ages. The data below will be hard coded into the program.
Mason: 25
Bubba: 44
Jill: 16
Pat: 18
Jenny: 21
Howard: 44
Buster: 8
The program will then repeatedly ask questions of the user and provide answers. First find out if the user is interested in a list of names of those who are exactly equal to a certain age (integer values only), older than a certain age, or younger than a certain age. This should be accomplished by asking the user to enter a single character response to a single question. If the user enters an invalid response, output an error message and retreat to the question again. If the user enters the single character “X,” then end the program. Use Scanner rather than JOptionPane for all input and output.
If a valid character is entered for the question, then ask for an age for comparison. Bonus points for error checking in this step (not required) - if you do, indicate via Java code comments. Display on the console all the names and ages that satisfy the question. Display each on a separate line using the format “name : age.” If no name satisfy the question, indicate in a message to the console. After this step, always retreat to asking a new question – do not terminate the program.
Aucun commentaire:
Enregistrer un commentaire