0
HELP!!!
How do I write a program that allows you to enter the name of each person in the room. Once all the data has been entered, the program will display each person's name (one name per line).
5 Respostas
+ 1
Add following lines behind //save name in data structure
//input age
// save age in data structure 2
It's important to use now a second data structure. Both data structures should be from the same type and they should be ordered. So a Set is now not possible anymore.
// more advanced..
There is a way to use only one data structure. Therefore you have to declare an object "Guest" with two attriobutes String name and int age. Write a constructor and make them public accessable.
Now you can store your "Guest" objects into a single data structure.
+ 1
Use Scanner for input and wait for a specific value to be entered. Then u can use a while loop for the input. After input the input can be stored in a LinkedList or a Set or similar which has a dynamic size. After your specific exit code was entered you go through your data structure by using a for each loop and output the current Object. Here is a structure:
public static void main(String[] args) {
//Scanner init
// initialize data structure "ds" (e.g. LinkedList)
while( /*scanner input is not equal to exit word" ){
// input name
// save name in data structure
}
for( String s : ds ){
// print s
}
}
+ 1
GENIUS!! You are amazing AKC i just ran it and it works perfect, ive been working on this for ages. Ok LAST question, how do i modify this so that the program also displays if a person is above/equal or below the average age? im thinking using conditional statements?
+ 1
Just add as you thought conditionals (if-elseif-else) to your output
0
Ok thanks for that, now how would I modify this code so that the program also asks for the person's age and when displaying, shows the age next to the person's name?