第一步我们首先查看一下Math数学函数的API,可以看到pow()方法返回第一个参数的第二个参数次方,格式为Math.pow(m,n),代表m的n次方,如下图所示:
//获取4位短信验证码
public static String getVerCode(Integer len) {
Random random = new Random();
double c=Math.pow(10,len);
String fourRandom = random.nextInt(new Double(c).intValue()) + "";
int randLength = fourRandom.length();
if (randLength < len) {
for (int i = 1; i <= len - randLength; i++)
fourRandom = "0" + fourRandom;
}
return fourRandom;
}

656

被折叠的 条评论
为什么被折叠?



