public void run() {
StringBuilder sb = new StringBuilder();
// 生成随机数六位
int[] temp = new int[6];
for (int i = 0; i < 6; i++) {
temp[i] = (int) (Math.random() * 10);
sb.append(temp[i]);
}
String str = sb.toString();
System.out.println("6位数" + str);
//生成的时间
StringBuilder actualTime = new StringBuilder();
//1判断每一位
String[][] confirm = new String[][]{{"2", "1", "0"}, {"3", "2", "1", "0"}, {"5", "4", "3", "2", "1", "0"}, {}, {"5", "4", "3", "2", "1", "0"}, {}};
String actualTimeStr = "";
for (int s = 0; s < confirm.length; s++) {
if (s != 1 || (actualTimeStr.equals("2"))) {
aa:
for (int l = 0; l < confirm[s].length; l++) {
for (int i = 0; i < str.length(); i++) {
if ((str.charAt(i) + "").equals(confirm[s][l])) {
actualTime.append(confirm[s][l]);
sb.delete(i, i + 1);
str = sb.toString();
break aa;
}
}
}
}
if (confirm[s].length == 0 || ((!actualTimeStr.equals("2")) && s == 1)) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
int charLength = chars.length - 1;
int maxIndex = str.indexOf(chars[charLength]);
actualTime.append(chars[charLength]);
sb.delete(maxIndex, maxIndex + 1);
str = sb.toString();
}
if (actualTime.length() != (s + 1)) {
actualTime.append("0");
}
actualTimeStr = actualTime.toString();
System.out.println("第" + (s + 1) + "位" + actualTime.toString());
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < actualTimeStr.length(); i++) {
result.append(actualTimeStr.charAt(i));
if ((i + 1) % 2 == 0) {
result.append(":");
}
}
System.out.println(result.toString());
}
随机生成6个0-9的随机数,组成一个最大时间。当产生不合理数字就做0处理
于 2022-04-11 09:53:45 首次发布