Member 13492392 Ответов: 1

Java: как я могу хранить в массиве имена и фамилии каждого члена семьи в моем коде?


import java.util.Arrays;
import javax.swing.JOptionPane; // Imports information to use JOptionpane class

public class Membership2SD {

    public static void main(String[] args) {
        // Initize
        int cont;             // Continue 
        String name;          // Family's last name 
        String family[];      // array that will contain all family members 
        
        do {
            // Ask for the family name
            name = JOptionPane.showInputDialog(null, 
                    "What is the name of the family?");
            
            // Store family names into array
            family = storeFamily(name);
            
            // TESt ONLY - Once you verify that you have entered the data
            // correctly into the array, you would comment this code out 
            // Print the family list as entered - DO NOT COMMENT OUT.
            printFamily(family);
            
            // Sort the family 
            family = sortFamily(family);
            
            // Print the sorted family list 
            printFamily(family);
            
            // Ask if there is another family 
            cont = JOptionPane.showMessageDialog(null,
                    "Do you want to add another family?", 
                    "Membership", JOptionPane.YES_NO_OPTION);
        } while (cont == JOptionPane.YES_OPTION);
        
        JOptionPane.showMessageDialog(null, "Please come again");
    } // End of 'main' method
    
    public static String[] storeFamily(String name) {
       String ans;
       int count;
       String first;
      
       ans = JOptionPane.showInputDialog(null, "How many members in the " + name +
                " Family?");
       
       count = Integer.parseInt(ans);
       
       String[] family = new String[count];
       
       
       for (int i = 1; i < count; count++) {
           
        first = JOptionPane.showInputDialog(null, "Enter first name: " + count);
        
        String [] Family = new String [count];
       }
       
       return family;
}

Assignment: 

Create a method called storeFamily that will:
Passed the family's last name. (completed)
Ask for the number of family members. (completed)
Create a new String array based on the number of family members. Use String[] family = new String[count]; Where count is the number of family members. (Completed) 
Ask the user for the first names. (completed) 
Store in the array the first and last names of each family member. (incomplete)
Return the array containing the family members. (complete) 

Everything that's in the main method is code that I can't touch. I have to create the methods to make the code in the main work.

What I have tried:

I am pretty lost on how to use an array. I've been searching videos on youtube I don't quite get it.

1 Ответов

Рейтинг:
6

Peter Leow

Вы должны использовать

Array of Objects
- массив, содержащий объекты в виде элементов.
Например, создайте класс Person со свойствами firstName, lastName, relationship и т. д.
Попросите каждого члена семьи предоставить входные данные по этим свойствам, а затем добавьте его/ее как новый объект Person в массив.
Проверить это:Массив объектов - Java Tutorial - Java With Us[^]
+++++[Раунд 2]+++++
В ответ на ваш дальнейший запрос в комментарии, showMessageDialog[^] только для информации, он ничего не возвращает, вы должны использовать showOptionDialog[^]