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("市辖区", 110100);
areaCode.put("东城区", 110101);
areaCode.put("西城区", 110102);
areaCode.put("崇文区", 110103);
areaCode.put("宣武区", 110104);
areaCode.put("朝阳区", 110105);
//地区号
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());
======
执行响应结果:
在其他地方直接引用${idNumber}即可
另一种方法参考:
http://www.51testing.com/html/14/n-6657714.html?nomobile=1