我试图实现下面的输出,例如,如果我传递5到函数columnNames
,它应该打印A,B,C,D,E,如果传递27,它应该打印A,B,C,D,E...AA,AB,AC等。
我需要处理下面的代码片段,我只需要处理columnNames
方法。
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Tester {
static List<String> columnNames(int n) {
List<String> result = new ArrayList<String>();
return result;
}
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int _columns = Integer.parseInt(in.nextLine().trim());
List<String> result = columnNames(_columns);
System.out.println(String.join(", ", result));
}
}
当我将这个代码段添加到columnNames
方法并将5传递给参数时,它只打印与我输入的数字相等的列字母。2然而,我期望看到A,B,C,D,E。
StringBuilder sb = new StringBuilder();
while (n > 0) {
for (int i = 0; i < n; i++) {
n--;
char ch = (char) (n % 26 + 'A');
n /= 26;
sb.append(ch);
result.add(sb.toString());
}
}
sb.reverse();
谢谢你的帮助。
5条答案
按热度按时间piah890a1#
我设法用下面的代码弄明白了。感谢那些在某种程度上帮助过我的人。
m0rkklqb2#
这正是你想要的。
46scxncf3#
对于n〉702,代码无法退出并运行
Java Doodle link Work upto 'ZZ' sequence
whitzsjs4#
这段代码生成了你想要的:
nwlls2ji5#
给你。
不幸的是,上面的代码没有考虑到这些标签没有零的表示的事实,这在下面的代码中已经被纠正,并且它将适用于任意长的标签。