简单通讯录管理系统java头歌
时间: 2023-11-02 09:05:59 浏览: 95
对不起,作为AI语言模型,我不会唱歌,但是我可以为您提供一个简单通讯录管理系统java代码:
import java.util.Scanner;
public class ContactList {
public static void main(String[] args) {
Contact[] contacts = new Contact[10]; //创建一个包含10个联系人的数组
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println("请选择操作:");
System.out.println("1.添加联系人");
System.out.println("2.查找联系人");
System.out.println("3.删除联系人");
System.out.println("4.显示所有联系人");
System.out.println("0.退出程序");
choice = input.nextInt();
switch(choice) {
case 1:
addContact(contacts);
break;
case 2:
searchContact(contacts);
break;
case 3:
deleteContact(contacts);
break;
case 4:
displayContacts(contacts);
break;
case 0:
System.out.println("程序已退出。");
break;
default:
System.out.println("输入错误,请重新输入。");
}
} while(choice != 0);
}
//添加联系人
public static void addContact(Contact[] contacts) {
Scanner input = new Scanner(System.in);
System.out.println("请输入联系人姓名:");
String name = input.nextLine();
System.out.println("请输入联系人电话号码:");
String phone = input.nextLine();
System.out.println("请输入联系人电子邮件地址:");
String email = input.nextLine();
Contact contact = new Contact(name, phone, email);
for(int i = 0; i < contacts.length; i++) {
if(contacts[i] == null) {
contacts[i] = contact;
System.out.println("联系人已添加。");
break;
}
}
}
//查找联系人
public static void searchContact(Contact[] contacts) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要查找的联系人姓名:");
String name = input.nextLine();
boolean found = false;
for(int i = 0; i < contacts.length; i++) {
if(contacts[i] != null && contacts[i].getName().equals(name)) {
System.out.println(contacts[i]);
found = true;
break;
}
}
if(!found) {
System.out.println("未找到该联系人。");
}
}
//删除联系人
public static void deleteContact(Contact[] contacts) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要删除的联系人姓名:");
String name = input.nextLine();
boolean deleted = false;
for(int i = 0; i < contacts.length; i++) {
if(contacts[i] != null && contacts[i].getName().equals(name)) {
contacts[i] = null;
System.out.println("联系人已删除。");
deleted = true;
break;
}
}
if(!deleted) {
System.out.println("未找到该联系人。");
}
}
//显示所有联系人
public static void displayContacts(Contact[] contacts) {
for(int i = 0; i < contacts.length; i++) {
if(contacts[i] != null) {
System.out.println(contacts[i]);
}
}
}
}
class Contact {
private String name;
private String phone;
private String email;
public Contact(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "姓名:" + name + ",电话号码:" + phone + ",电子邮件地址:" + email;
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)