日期:2014-05-18  浏览次数:20619 次

解释一段代码
String   randomKey(int   sLen)   {
        String   base;
        String   temp;
        int   i;
        int   p;
        base   =   "1234567890 ";
        temp   =   " ";
        for   (i   =   1;   i   <   sLen;   i++)   {
                p   =   (int)   (Math.random()   *   10);
                temp   +=   base.substring(p,   p   +   1);
        }
        return   (temp);
}
随即产生数     但具体每句话什么意思?谢谢   简单的说说

------解决方案--------------------
产生sLen长度的随机数串

(int) (Math.random() * 10); //返回0-9中任一随机数

temp += base.substring(p, p + 1);//从base串中取第p位,串接起来
------解决方案--------------------
String base;
String temp;
int i;
int p;
base = "1234567890 ";
temp = " ";
for (i = 1; i < sLen; i++) { //sLen返回的数字的长度
p = (int) (Math.random() * 10); //随机生成一个0到9之间的数字p
p = 9;
temp += base.substring(p, p + 1); //从base中取p位置上的单个字符,并连接到temp上面
}
return (temp);