Created
October 14, 2012 04:44
-
-
Save fanker/3887373 to your computer and use it in GitHub Desktop.
创建一个手机类,可以通过手机获得指定联系人的电话号码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobile; | |
public class Mobile { | |
private String[] names = new String[10];//定义数组,开辟内存空间 | |
private String[] nums = new String[10];//定义数组,开辟内存空间 | |
java.util.Scanner input = new java.util.Scanner(System.in); | |
//开始方法 | |
public void star(){ | |
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | |
System.out.println("请选择:"); | |
System.out.println("1.存入电话号码"); | |
System.out.println("2.查询电话号码"); | |
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); | |
String num = input.next(); | |
if ("1".equals(num)){ | |
save(); | |
}else if ("2".equals(num)){ | |
search(); | |
} else { | |
System.out.println("输入错误,请重新选择!"); | |
} | |
star(); | |
} | |
//存储方法 | |
public void save(){ | |
System.out.println("请输入姓名:"); | |
String name = input.next(); | |
System.out.println("请输入电话号码:"); | |
String telNum = input.next(); | |
int index = -1;//引进临时变量,来判断内存使用情况 | |
for (int i = 0; i < names.length; i++){ | |
if (names[i] == null){ | |
names[i] = name; | |
nums[i] = telNum; | |
index = i; | |
break; | |
} | |
} | |
if (index == -1){ | |
System.out.println("内存已满!"); | |
} | |
star(); | |
} | |
//查询方法 | |
public void search(){ | |
System.out.println("请输入要查询的姓名"); | |
String name = input.next(); | |
int index = -1;//引进临时变量,来判断内存使用情况 | |
for (int i = 0; i < names.length; i++){ | |
if (names[i] != null&&names[i].equals(name)){ | |
index = i; | |
break; | |
} | |
} | |
if (index != -1){ | |
System.out.println("你要查询的电话号码为:" + nums[index]); | |
}else { | |
System.out.println("要查询的电话号码不存在"); | |
} | |
star(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobile; | |
public class Test { | |
public static void main(String[] args) { | |
Mobile m = new Mobile(); | |
m.star(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment