2025-05-10 11:51:35 +08:00

53 lines
1.5 KiB
Java

package com.zhgd.xmgl.util;
import java.util.HashSet;
import java.util.Set;
public class CarUtils {
/**
* 随机获取车牌号
*
* @return
*/
public static String randomPlateNO() {
String plateNO = "";
//省号
String[] sheng = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", ""};
int i = (int) (Math.random() * (sheng.length));
plateNO += sheng[i];
//字母
plateNO += getA();
//字母总数
int Anum = (int) (Math.random() * 3);
Set<Integer> set = new HashSet<>();
while (set.size() != Anum) {
set.add((int) (Math.random() * 5));
}
//插入编号
for (int k = 0; k < 5; k++) {
if (set.contains(k)) {
plateNO += getA();
} else {
plateNO += (int) (Math.random() * 10);
}
}
return plateNO;
}
//获取一个随机字母
private static String getA() {
int j = 0;
do {
j = Integer.valueOf('A') + (int) (Math.random() * 26);
}
while ((j == 73) || (j == 79));
return "" + (char) j;
}
public static void main(String[] args) {
System.out.println(randomPlateNO());
System.out.println(randomPlateNO());
}
}