1 题目要求
通讯录中的一条记录包含下述基本信息:朋友的姓名、出生日期、性别、固定电话号码、移动电话号码。 本题要求编写程序,录入N条记录,并且根据要求显示任意某条记录。
输入格式:
输入在第一行给出正整数N(≤10);随后N行,每行按照格式姓名 生日 性别 固话 手机给出一条记录。其中姓名是不超过10个字符、不包含空格的非空字符串;生日按yyyy/mm/dd的格式给出年月日;性别用M表示“男”、F表示“女”;固话和手机均为不超过15位的连续数字,前面有可能出现+。
在通讯录记录输入完成后,最后一行给出正整数K,并且随后给出K个整数,表示要查询的记录编号(从0到N−1顺序编号)。数字间以空格分隔。
输出格式:
对每一条要查询的记录编号,在一行中按照姓名 固话 手机 性别 生日的格式输出该记录。若要查询的记录不存在,则输出Not Found。
2 样例
输入样例:
3
Chris 1984/03/10 F +86181779452 13707010007
LaoLao 1967/11/30 F 057187951100 +8618618623333
QiaoLin 1980/01/01 M 84172333 10086
2 1 7
输出样例:
LaoLao 057187951100 +8618618623333 F 1967/11/30
Not Found
3 分析
(1)面向对象编程,
使用Person封装个人信息,将属性私有,对外提供访问和操作属性的方法。
使用PersonDao 对Person 对象数组进行操作
类图,使用ProcessOn 绘制
2) 需要注意的是,输出的属性顺序与输入的顺序不一致。
4 代码
import java.util.*;
class Person
{
private String name;
private String birthday;
private String gender;
private String phone;
private String mobile;
Person(String name,String birthday,String gender,String phone,String mobile)
{
this.name=name;
this.birthday=birthday;
this.gender=gender;
this.phone=phone;
this.mobile=mobile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone=phone;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile=mobile;
}
}
class PersonDao
{
public void find(Person []ps,int index)
{
int findFlag=0;
for(int i=0;i<ps.length;i++)
{
if(i==index)
{
findFlag=1;
System.out.println(ps[i].getName()+" "+ps[i].getPhone()+" "+ps[i].getMobile()+" "+ps[i].getGender()+" "+ps[i].getBirthday());
break;
}
}
if(findFlag==0)
System.out.println("Not Found");
}
}
class Main
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
Person []ps=new Person[num];
PersonDao dao=new PersonDao();
String name=new String();
String birthday=new String();
String gender=new String();
String phone=new String();
String mobile=new String();
//输入 联系人信息
for(int i=0;i<num;i++)
{
name=sc.next();
birthday=sc.next();
gender=sc.next();
phone=sc.next();
mobile=sc.next();
Person p=new Person(name,birthday,gender,phone,mobile);
ps[i]=p;
}
//输入查询人数
int count=sc.nextInt();
int []indices=new int[count];
for(int i=0;i<count;i++)
{
indices[i]=sc.nextInt();
}
for(int i=0;i<count;i++){
dao.find(ps,indices[i]);
}
}
}
5 总结
(1)类图 链接: 类图的类型和绘制.
(2)Scanner 的next() nextInt() nextLine()函数