作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一种方法,可以在给定列之后找到下一列。例如:
input: A
output: B
public static char nextLetter(char c) {
c++;
return c;
}
Z
列之后来
AA
, 之后
AZ
来
BA
, 之后
ZZ
来
AAA
等。我的下一个想法是首先根据索引找出列的位置。所以专栏
AA
将 27,
BA
52等
public static char getLetter(int index) {
return (char) (index + 64);
}
private static void getNotation(int size) {
int divided = size / 26;
int remainder = size % 26;
String notation = "";
while (divided % 26 > 0) {
// Here is where the need for a recursive method comes in
}
}
最佳答案
我创建了一个示例:
class Test {
static char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
private static String indexToColumnName(int i) {
if (i >= alphabet.length) {
return indexToColumnName((int)Math.floor(i / alphabet.length) - 1)
+ indexToColumnName(i % alphabet.length);
}
return Character.toString(alphabet[i]);
}
public static void main(String args[]) {
for (int i = 0; i <= 800; ++i) {
System.out.println(i + ": " + indexToColumnName(i));
}
}
}
0: A
1: B
2: C
3: D
...
24: Y
25: Z
26: AA
27: AB
...
700: ZY
701: ZZ
702: AAA
703: AAB
...
关于java - 如何将整数转换为 A1 表示法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59401548/
我是一名优秀的程序员,十分优秀!