输入变量与实际变量相匹配
import java.util.Scanner;
public class Text1
{
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
connect b=new connect();
System.out.println("请输入联系人数量");
int n=sc.nextInt();
System.out.println("请输入"+n+"个联系人姓名和电话");
for(int i=1;i<=n;i++)
{
String name=sc.next();
String number=sc.next();
System.out.println(b.add(name,number));
}
System.out.println("全部的联系人为");
b.look();
System.out.println("请输入查找姓名");
String seekname=sc.next();
if((b.seek(seekname))==null)
System.out.println("未找到");
System.out.println((b.seek(seekname)).name+" "+(b.seek(seekname)).number);
System.out.println("请输入删除姓名");
String deletename=sc.next();
System.out.println( b.delete(deletename));
System.out.println("全部的联系人为:");
b.look();
}
}
class Person
{
String name;
String number;
}
class connect
{
Person a[];
int number;
public connect()
{
a = new Person[100];
number = 0;
}
public String add(String name,String number)
{
if(this.number>99)
return "添加失败";
a[this.number]=new Person();
a[this.number].name=name;
a[this.number].number=number;
this.number++;
return "添加成功";
}
public Person seek(String name)
{
for(int i=0;i<number;i++)
{ if((a[i].name).equals(name))//输入变量与实际变量相匹配
{
return a[i];
}
}
return null;
}
public void look()
{
for(int i=0;i<number;i++)
{
System.out.println("联系人 "+a[i].name+" 电话 "+a[i].number);
}
}
public String delete(String name)
{
for(int i=0;i<number;i++)
{ if((a[i].name).equals(name))
{
a[i]=a[this.number-1];
number=number-1;
return "删除成功";
}
}
return "删除失败";
}
}