传入一个数字,根据这个数字大小来判断要返回跟这个数度大小一样长度的流水号

来源:百度知道 编辑:UC知道 时间:2024/05/25 16:48:52
如题,要生成一个流水号,但用户可以传入一个数字,以便根据这个数字来判断要生成的流水号的长度

class Test {
public static long genSN1(int length) {
long max = (long) Math.pow(10, length) - 1;
return Math.round(Math.random() * max);
}

public static String genSN2(int length) {
String sn = "";
for (int i = 0; i < length; i++) {
int p = (int)Math.round(Math.random() * 9);
sn += p;
}
return sn;
}

public static String genSN3(int length) {
String sn = String.valueOf(System.currentTimeMillis());
if (sn.length() > length) {
return sn.substring(sn.length() - length);
}
for (int i = sn.length(); i < length; i++) {
int p = Math.round(Math.round(Math.random() * 9));
sn += p;
}
return sn;
}

public static void main(String[] args) {
int length = 25;
System.out.println("sn1: " + Test.genSN1(length));
System.out.