package com.company;
import com.sun.corba.se.spi.orbutil.fsm.Input;
import java.awt.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String x;
ArrayList names=new ArrayList();
ArrayList numbers=new ArrayList();
Scanner in = new Scanner(System.in);
System.out.println("存储或查找");
String a=in.nextLine() ;
if( a.equals("存储")) {
do {
System.out.println("姓名?");
String n = in.nextLine();
names.add(n);
System.out.println("联系方式?");
String p = in.nextLine();
numbers.add(p);
System.out.println("是否还要继续保存? (yes)");
x = in.nextLine() ;
}
while (x.equals("yes"));
System.out.println("是否要查找?(yes)");
if (in.nextLine(). equals("yes")){a="查找";}
}
if (a.equals("查找") ){
System.out.println("输入查找对象");
Object O = in.next();
int value = names.indexOf(O);
System.out.println("联系方式是" + numbers.get(value));
}
else {
System .exit(0);
}
}
}
小绵羊
import java.util.ArrayList;
import java.util.Scanner;
public class MirrorTelBook {
// 声明存储名字列表
ArrayList<String> nameList = new ArrayList<String>();
// 声明存储电话号码列表
ArrayList<String> numberList = new ArrayList<String>();
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"));
}
}
转载于:https://my.oschina.net/jingzigege/blog/649369