Java:如何在字母中进行增量

Tha*_*ham 0 java

我将在 Java 中读取两个字符的字符串。我想确定它的下一个增量是什么。下面是增量规则。

AA -> AB -> AC -> ... -> AZ -> BA -> BB -> ... -> ZZ -> AA
Run Code Online (Sandbox Code Playgroud)

所以如果我读入AC,我就会打印出来AD

编辑

我可以增加一个字符,就像这样System.out.println((char) ('C' + 1));。所以我正在考虑解析字符串,获取单个字符,只需增加或减少 char 的值。环绕是让我着迷的,比如AZ-> BA。还不确定实现这一目标的最佳方法是什么。你有什么想法

Mar*_*lin 5

public static String increment(final String p_source) {
    final int first = (p_source.charAt(0) - 'A') * 26;
    final int second = p_source.charAt(1) - 'A';

    final int next = (first + second + 1) % (26*26); 

    return new String(new byte[] {(byte)(next / 26 + 'A'), (byte)(next % 26 + 'A')});
}
Run Code Online (Sandbox Code Playgroud)