[Java] 纯文本查看 复制代码 package com.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String phoneNum=input();
String code=getCode();
Map<String,String> map=new HashMap<String,String>();
map.put(phoneNum, code);
for (Map.Entry<String, String> e : map.entrySet()) {
System.out.println("电话号:"+e.getKey()+"\t"+"验证码:"+e.getValue());
}
}
public static String input() {
String str = "^1[34578]{1}[0-9]{9}$";
Pattern p = Pattern.compile(str);
Scanner sc = new Scanner(System.in);
System.out.print("请输入手机号:");
while (true) {
String phoneNum = sc.next();
if (p.matcher(phoneNum).matches()) {
return phoneNum;
} else {
System.out.println("手机号码不会合法!");
System.out.print("请输入手机号:");
}
}
}
public static String getCode() {
StringBuffer bu = new StringBuffer();
for (int i = 0; i < 4; i++) {
bu.append((int) (Math.random() * 10));
}
return bu.toString();
}
}
|