版权声明:本文为博主原创文章,未经博主credreamer 允许不得转载 违者追究法律责任。 https://blog.csdn.net/lidew521/article/details/83754845
第一步我们首先查看一下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; }