Java:如何在代码中存储每个家庭成员的名字和姓氏? [英] Java: how do I store in an array first and last names of each family member in my code?

查看:150
本文介绍了Java:如何在代码中存储每个家庭成员的名字和姓氏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 import java.util.Arrays; 
import javax.swing.JOptionPane; //导入信息以使用JOptionpane类

公共类Membership2SD {

public static void main(String [] args){
// Initize
int cont; //继续
字符串名称; //姓氏
String family []; //将包含所有家庭成员的数组

do {
//要求姓氏
name = JOptionPane.showInputDialog(null,
什么是这个家庭的名字?);

//将族名存储到数组
family = storeFamily(name);

//仅限TESt - 一旦您确认已将数据
//正确输入数组,就会将此代码注释掉
//将族列表打印为输入 - 不要评论。
printFamily(family);

//对家庭进行排序
family = sortFamily(family);

//打印已排序的家庭列表
printFamily(family);

//询问是否有另一个家庭
cont = JOptionPane.showMessageDialog(null,
你想要添加另一个家庭吗?,
会员资格 ,JOptionPane.YES_NO_OPTION);
} while(cont == JOptionPane.YES_OPTION);

JOptionPane.showMessageDialog(null,Please come again);
} //'main'方法结束

public static String [] storeFamily(String name){
String ans;
int count;
字符串优先;

ans = JOptionPane.showInputDialog(null,+ name +
系列中有多少成员?);

count = Integer.parseInt(ans);

String [] family = new String [count];


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

first = JOptionPane.showInputDialog(null,输入名字:+计数);

String [] Family = new String [count];
}

返回家庭;
}

作业:

创建一个名为storeFamily的方法,它将:
传递该姓氏的姓氏。 (已完成)
询问家庭成员人数。 (已完成)
根据家庭成员的数量创建一个新的String数组。使用String [] family = new String [count];其中count是家庭成员的数量。 (已完成)
向用户询问名字。 (已完成)
在数组中存储每个家庭成员的名字和姓氏。 (incomplete)
返回包含家庭成员的数组。 (完整)

主要方法中的所有内容都是我无法触及的代码。我必须创建方法来使代码在主要工作中。

我尝试了什么:

我对如何使用数组感到很遗憾。我一直在youtube上搜索视频我不太明白。

解决方案

你应该使用

对象数组

- 包含对象作为元素的数组。

例如,创建一个名为Person的类,其属性包括firstName,lastName,relationship等。

让每个家庭成员提供有关这些属性的输入,然后将他/她添加为数组中的新Person对象。

检查出来:对象阵列 - Java教程 - Java With Us [ ^ ]

+++++ [第2轮] +++++

在评论中回复您的进一步查询, showMessageDialog [ ^ ]仅供参考只是,它不会返回任何内容,你应该使用 showOptionDialog [ ^


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.

解决方案

You should be using

Array of Objects

- an array that contains objects as elements.
For example, create a class called Person with properties such as firstName, lastName, relationship, etc.
Get each family member to provide input on these properties, then add him/her as a new Person object in the array.
Check this out:
Array of Objects - Java Tutorial - Java With Us[^]
+++++[Round 2]+++++
In reply to your further query in the comment, showMessageDialog[^] is only for information only, it does not return anything, you should be using showOptionDialog[^]


这篇关于Java:如何在代码中存储每个家庭成员的名字和姓氏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆