0

在我的代码中,我试图让它计算随机字符串的下一个值,其中字符串遵循以下格式:

啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊!

下一个值将是 AAA-AAAA-AAA-AAB,到达 AAA-AAAA-AAA-AAF,然后再到达 AAA-AAAA-AAA-AA1,依此类推,直到达到 9。

从那里,它应该继续并增加它前面的字母,使其成为 AAA-AAAA-AAA-ABA,并从头开始整个过程​​。

我将如何在 Java 中实现这一点?我正在考虑递归,但我不知道从哪里开始。

4

4 回答 4

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;
}
于 2013-05-15T21:16:50.480 回答
0

如果您有需要遵循的固定格式,则可以使用基于 15 的计数器(值 A-F1-9),也可以增加格式化值。您所需要的只是一个从末尾开始的简单循环。如果您有一个小于 Z 的值递增并停止,如果您有 Z,则返回 A 并后退一步。(携带)

顺便说一句,这听起来像是修改后的十六进制格式。这是你想要的吗。

于 2013-05-15T21:13:04.930 回答
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()、.valueOf() 等,以您喜欢的任何格式返回每个数字的值。

于 2013-05-15T21:32:09.457 回答
0

如果你只是想加一,你应该能够通过按顺序定义一个带有数字的字符串,然后从后到前循环遍历你的随机字符串,跳过所有不在你定义的数字中的字符串并递增. 这应该适用于一组数字的任意定义。

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();
}
于 2013-05-15T21:51:25.563 回答