package test;import java.util.ArrayList;import java.util.Scanner;public class MirrorTelBook { // 声明存储名字列表 ArrayListnameList = new ArrayList (); // 声明存储电话号码列表 ArrayList numberList = new ArrayList (); public static Scanner in = new Scanner(System.in); public static void main(String[] args) { MirrorTelBook instance = new MirrorTelBook(); do { System.out.println("欢迎使用镜子电话簿!"); int operation = 0; System.out.println("功能选择:1、添加号码 2、查询号码 0、结束"); operation = in.nextInt(); switch (operation) { case 0: break; case 1: instance.saveNum(); break; case 2: instance.fetchNum(); break; } System.out.println("是否还要继续? (yes/no)"); } while (in.next().equals("yes")); System.exit(0); System.out.print("结束!"); } public void saveNum() { do { System.out.println("姓名?"); String n = in.next(); nameList.add(n); System.out.println("联系方式?"); String p = in.next(); numberList.add(p); System.out.println("是否还要继续保存? (yes/no)"); } while (in.next().equals("yes")); } public void fetchNum() { do { System.out.println("请输入查询名字!"); // 如果为0 表示没找到 1 表示找到 int flag = 0; String name = in.next(); for (int i = 0; i < nameList.size(); i++) { if (nameList.get(i).equals(name)) { System.out.println(String.format("%s 电话号码为:%s!", name, numberList.get(i))); flag = 1; break; } } if (flag == 0) { System.out.print(String.format("没有找到%s电话号码!", name)); } System.out.println("是否还要继续查找? (yes/no)"); } while (in.next().equals("yes")); }}