https://www.codenong.com/cs107088979/
代碼如下:
import java.util.*;
StringBuilder generater = new StringBuilder();
int sex=0; // 1為男 0 為女
int age=1979; //1979為大于18歲 2000小于18歲
Map areaCode = new HashMap();
areaCode.put("北京市", 110000);
areaCode.put("市轄區(qū)", 110100);
areaCode.put("東城區(qū)", 110101);
areaCode.put("西城區(qū)", 110102);
areaCode.put("崇文區(qū)", 110103);
areaCode.put("宣武區(qū)", 110104);
areaCode.put("朝陽區(qū)", 110105);
//地區(qū)號
String randomAreaCode="";
int index = (int) (Math.random() * areaCode.size());
Collection values = areaCode.values();
Iterator it = values.iterator();
int i = 0;
int code = 0;
while (i < index && it.hasNext()) {
i++;
randomAreaCode = it.next().toString();
}
generater.append(randomAreaCode);
//生日
String randomBirthday="";
Calendar birthday = Calendar.getInstance();
birthday.set(Calendar.YEAR, (int) (Math.random() * 20) + age);
birthday.set(Calendar.MONTH, (int) (Math.random() * 12));
birthday.set(Calendar.DATE, (int) (Math.random() * 31));
StringBuilder builder = new StringBuilder();
builder.append(birthday.get(Calendar.YEAR));
long month = birthday.get(Calendar.MONTH) + 1;
if (month < 10) {
builder.append("0");
}
builder.append(month);
long date = birthday.get(Calendar.DATE);
if (date < 10) {
builder.append("0");
}
builder.append(date);
randomBirthday= builder.toString();
generater.append(randomBirthday);
//隨機碼
String randomCode="";
int code = (int) (Math.random() * 1000);
if (code < 10) {
// randomCode= "00" + code;
if(code%2==sex)
{
randomCode= "00" + code;
}
else
{
code=code + 1;
randomCode= "00" + code;
}
} else if (code < 100) {
// randomCode= "0" + code;
if(code%2==sex)
{
randomCode= "0" + code;
}
else
{
code=code + 1;
randomCode= "0" + code;
}
} else {
// randomCode= "" + code;
if(code%2==sex)
{
randomCode= "" + code;
}
else
{
code=code + 1;
randomCode= "" + code;
}
}
generater.append(randomCode);
//驗證碼
char[] chars= generater.toString().toCharArray();
int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
int[] n = new int[17];
int result = 0;
for (int i = 0; i < n.length; i++) {
n[i] = Integer.parseInt(chars[i] + "");
}
for (int i = 0; i < n.length; i++) {
result += c[i] * n[i];
}
char validateCode = r[result % 11];
generater.append(validateCode);
vars.put("idNumber",generater.toString());
SampleResult.setResponseData(generater.toString());
======
執(zhí)行響應結果:
在其他地方直接引用${idNumber}即可
另一種方法參考:
http://www.51testing.com/html/14/n-6657714.html?nomobile=1