Ⅰ 實現一個小型通訊錄。java
Friend類:public class Friend {
/*
* 姓名
*/
private String name;
/*
* 電話
*/
private String telephone;
/*
* 郵箱
*/
private String email;
/*
* 公司
*/
private String company; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String toString() {
StringBuffer str = new StringBuffer(); str.append("姓名:" + name).append("\n");
str.append("電話:" + telephone).append("\n");
str.append("郵箱:" + email).append("\n");
str.append("公司:" + company).append("\n");
str.append("-----------------------------------------\n");
return str.toString();
}
}AddFriend類:public class AddFriend { /**
* 主方法 程序的入口
*/
public static void main(String[] args) {
List<Friend> friendList = new ArrayList<Friend>();
char isGo = 'Y';
int i = 0;
do {
Friend friend = new Friend();
System.out.println("請輸入第" + (i + 1) + "位朋友的姓名:");
InputStreamReader reader = new InputStreamReader(System.in);
String str = "";
try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setName(str); System.out.println("請輸入第" + (i + 1) + "位朋友的電話:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str.matches("\\d*") && str.length() == 11) {// 判斷用戶輸入的電話是否符合標准
friend.setTelephone(str);
} else {
System.out.println("電話號碼輸入有誤,請重新輸入!");
continue;
} System.out.println("請輸入第" + (i + 1) + "位朋友的郵箱:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setEmail(str); System.out.println("請輸入第" + (i + 1) + "位朋友的公司:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setCompany(str); friendList.add(friend); i++; System.out.println("是否繼續添加?(Y/N):");
String go = "";
try {
go = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
isGo = go.charAt(0);
} while (isGo == 'Y' || isGo == 'y'); for (int j = 0; j < friendList.size(); j++) {
System.out.println(friendList.get(j).toString());
}
}
}
Ⅱ java如何判斷手機號碼是11為且第一位不是0且不是+86的形式
寫在前面:最好還是用正則表達式,簡單易懂,易於維護
===================================
//這個方法判斷是不是合法的手機號碼
public boolean isPhoneNumber(String phoneNumber){
//手機號碼長度
int phoneLength=phoneNumber.length();
//第一位是不是0
String phoneOne=phoneNumber.substring(0,1);
//是不是 +86形式
int is86=phoneNumber.indexOf("+86");
//是純數字 並且長度等於11 並且第一位不是0 並且 不包含+86
return isNumeric(phoneNumber)&&phoneLength==11&&!phoneOne.equals("0")&&is86==-1;
}
//這個方法判斷字元串是不是純數字
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
Ⅲ JAVA怎樣用數組實現輸入手機號碼為11位
順手寫了個,看下是不是你想要的。
publicclassT4{
Stringname=null;
int[]nums=null;
T4(Stringname,int[]nums){
this.name=name;
this.nums=nums;
}
publicstaticvoidmain(String[]args){
//正確示例
T4t=newT4("天王蓋地虎",newint[]{1,3,3,7,8,9,8,9,8,7,8});
if(t.isRight())
t.print();
//錯誤示例1
T4t1=newT4("張三",newint[]{1});
if(t1.isRight())
t1.print();
//錯誤示例2
T4t2=newT4("李四",newint[]{2,3,3,7,8,9,8,9,8,7,8});
if(t2.isRight())
t2.print();
//錯誤示例3
T4t3=newT4("王五",newint[]{1,1,3,7,8,9,8,9,8,7,8});
if(t3.isRight())
t3.print();
}
privatevoidprint(){
Stringphone="";
for(inti=0;i<nums.length;i++){
phone+=(i+"");
}
System.out.println("聯系人姓名:"+name+",電話:"+phone.trim());
}
privatebooleanisRight(){
if(nums.length!=11){
System.err.println(name+",的手機長度不對");
returnfalse;
}elseif(nums[0]!=1){
System.err.println(name+",的手機號碼第一位必須為1");
returnfalse;
}elseif(nums[1]!=3&&nums[1]!=5&&nums[1]!=7&&nums[1]!=8){
System.err.println(name+",的手機號碼第二位必須為3/5/7/8");
returnfalse;
}
returntrue;
}
}