我必须创建一个程序,允许用户输入包含姓名,地址,城市,州,邮政编码和年龄的信息。数据结构ArrayList将用于存储信息。创建用于创建对象的类和用于访问信息的类方法。
一个。显示您输入的所有记录
湾显示最老或最年轻的
℃。使用名字或姓氏查找记录。
public class people {
String name, address, city, state, zipcode, age;
public people() {
name = null;
address = null;
city = null;
state = null;
zipcode = null;
age = null;
}
public void setpeople(String n, String ad, String c, String s, String z, String a) {
name = n;
address = ad;
city = c;
state = s;
zipcode = z;
age = a;
}
public string AddRecord
public String getpeople() {
String p = "name:" + name + "\n" + "address:" + address + "\n" + "city:" + city + "\n" + "state:" + state + "\n" + "zip code:" + zipcode + "\n" + "age:" + age;
return p;
}
}
public class GroupArrayProject {
public static void main(String[] args) {
{
ArrayList < String > list = new ArrayList < String > ();
people two = new people();
Scanner one = new Scanner(System.in);
System.out.println("type in your name,address,city,state,zipcode and age ");
String a = one.next();
String b = one.next();
String c = one.next();
String d = one.next();
String e = one.next();
String f = one.next();
two.setpeople(a, b, c, d, e, f);
System.out.println(two.getpeople());
}
}
}
到目前为止,我还不知道下一步该怎么做
答案 0 :(得分:0)
将您的People类重命名为Person
zipcode和年龄应该是整数而不是字符串
在Person类中创建另一个构造函数,其中包含用于初始化实例数据的参数
为类中的每个实例变量添加getter和setter方法。
将以下内容添加到GroupArrayProject类:
private static List people = new ArrayList&lt;&gt;();
addRecord()应该在GroupArrayProject类中。
public void addRecord(Scanner input){
System.out.println("Name");
String name = input.next();
System.out.println("Address");
...
people.add(new Person(...));
}
这是基础知识。您只需要创建方法来搜索人员列表并找到匹配项,然后完成。