在我的代码中,我试图让它来计算随机字符串的下一个值,其中字符串遵循以下格式:
AAA-AAAA-AAA-AAA
下一个值是AAA-AAAA-AAA-AAB,达到AAA-AAAA-AAA-AAF,然后转到AAA-AAAA-AAA-AA1,依此类推,直到达到9。 / p>
从那里开始,它应该继续前进并增加它之前的字母,使其成为AAA-AAAA-AAA-ABA,并开始全程。
我将如何在Java中实现这一目标?我在想递归,但我不知道从哪里开始。
答案 0 :(得分:2)
最简单的方法是将字符串转换为整数并返回。从你的帖子我猜你的数字顺序是ABCDEF123456789
(0到14)所以我会用它。顺序0123456789ABCDEF
更常见,在这种情况下,您可以使用由库函数提供的十六进制。
private static final char[] digitMap = new char[] {'A','B','C','D','E','1','2','3','4','5','6','7','8','9'};
private static final int base = digitMap.length;
private static final Map<Character, Integer> reverseDigitMap = new HashMap<>();
static {
// populate the reverse digit map
for(int i=0;i<base;i++) {
reverseDigitMap.put(digitMap[i], i);
}
}
public static String toMyFormatString(long number) {
StringBuilder res = new StringBuilder();
// Add 13 digits to string in reverse order
for(int i=0;i < 13;i++) {
// Add dashes at correct locations
if(i==3 || i == 6 || i == 10) { res.append('-') };
// Output a character
res.append(digitMap[number % base]);
number = number / base;
}
// Change the order
res.reverse();
return res.toString();
}
/**
* @throws NullPointerException when an incorrect digit is encountered
**/
public static long fromMyFormatString(String numberString) {
long result = 0;
// Number is 16 characters of which 13 are digits
for(int i = 0; i < 16; i++) {
char digit = numberString.charAt(i);
// Skip "-"
if(digit == '-') { continue; };
result = result * base; // We're adding the next digit
result = result + reverseDigitMap.get(digit);
}
return result;
}
答案 1 :(得分:0)
如果您需要遵循固定格式,可以使用基于15的计数器(值A-F1-9),也可以增加格式化值。你需要的只是一个从最后开始的简单循环。如果您的值小于Z增量并停止,如果您有Z,则返回A并退回一个。 (进位)
BTW,这听起来像是修改后的十六进制格式。这是你的意图。答案 2 :(得分:0)
我的想法是枚举类型会使这更容易。像这样添加nextValue方法。在你的附加逻辑中使用它。只需调用.nextValue(),你就可以获得下一个“数字”应该是什么,如果.nextValue曾经变成“A”,那么你可以实现携带值的逻辑。表示每个总体“数字”的简单方法可能是列表。但是下面的内容有点放松了,因为从9到A的翻转都是内置的。
enum myEnum {
//had to use A1-A9 to represent the numbers because numbers themselves are not allowed
A, B, C, D, E, F, A1, A2, A3, A4, A5, A6, A7, A8, A9;
public myEnum nextValue() {
return this.ordinal() < myEnum.values().length - 1
? myEnum.values()[this.ordinal() + 1]
: A; //returns A if the value of this.ordinal = A9
}
};
您可以为枚举添加不同的方法,例如.toString(),. value_f()等,以您喜欢的任何格式返回每个数字的值。
答案 3 :(得分:0)
如果你只想尝试增加1,你应该能够通过按顺序定义一个带有数字的字符串,然后从后到前循环你的随机字符串,跳过所有不在你定义的字符串中来做到这一点。数字和递增。这应该适用于一组数字的任意定义。
public String incrementString(String randomString) {
// The ordering of digits.
String digits = "ABCDEF123456789";
// A StringBuffer to do a transform
StringBuffer inPlace = new StringBuffer(randomString);
// Loop through all of the digits of your random string...
for (int i = 0 ; i < inPlace.length() ; i++) {
// The index we are checking since we are traversing
// back to front.
int stringIndex = inPlace.length() - (i + 1);
// This is the index of the digit at 'stringIndex'
// in our 'digits' String
int digitIndex = digits.indexOf(stringIndex);
// If it's not there (meaning we have found a '-' character)
// just continue.
if (digitIndex == -1) {
continue;
// If it is not the last character in our 'digits' String, we can
// just replace and return.
} else if (digitIndex < digits.length() - 1) {
inPlace.setCharAt(stringIndex, digits.charAt(digitIndex + 1));
return inPlace.toString();
// If it IS the last character in our 'digits' String, we need to
// replace it with the first character in our 'digits' String and
// keep going.
} else {
inPlace.setCharAt(stringIndex, digits.charAt(0));
}
}
// In the event that every character in our random string was the
// last digit in our 'digits' String, (In this case, 999-9999-999-999)
// we will exit the loop without returning, so do it here.
return inPlace.toString();
}