for(int i=0; i<intArray.length; i++){ if(intArray[i] < 26) System.out.println(charArray[intArray[i]]); else{ int first = intArray[i]/26; int second = intArray[i]%26;
while(first > 26){ first = first/26; second = first%26; System.out.print(charArray[first]); } System.out.print(charArray[first]); System.out.print(charArray[second] +"\n"); } } } } 输出: A Q AA DW CCC
char c;int x = c - 'A' + 1
额,2397的结果我算出来不太一样额……………… 我和大虫前辈算出来都是CNE
不好意思 未经严格测试 改了下: public class LowToUpper { public static void main(String args[]){ char[] charArray = new char[27]; char temp = 'A';
for(int i=0; i<intArray.length; i++){ StringBuffer sb = new StringBuffer(); if(intArray[i] < 26) System.out.println(charArray[intArray[i]]); else{ int first = intArray[i]/26; int second; int third = intArray[i]%26;
sb.append(charArray[third]); while(first > 26){ second = first%26; first = first/26; sb.append(charArray[second]); } sb.append(charArray[first]); System.out.println(sb.reverse().toString()); } } } } 输出: A Q AA DW CNE ZYUAB
我给你一个最简单的 public class T { public static char get(int i) { if (i > 26 || i < 1) { throw new RuntimeException("请输入1-26中任意一个整数"); } return (char) (i + 64); } public static void main(String[] args) { System.out.println(get(1)); } }
public static void main(String[] args)throws Exception{
for(int i=1;i<40;i++){
System.out.print(toLetterString(i)+",");
}
}
public static String toLetterString(int x){
if(x<1){
//出错了.
return null;
}
if(x<27){
return String.valueOf((char)('A'+x-1));
}
if(x%26==0){
return toLetterString(x/26-1)+"Z";
}
return toLetterString(x/26)+String.valueOf((char)('A'+x%26-1));
}
}
结果:
F:\java>java Test7
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,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,
先输入个整数 再把它转换成 ASCII中的char类型呗 不过1好像不是对应 a 50是对应a
int length = s.length();
int result = 0;
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (Character.isUpperCase(c)) {
result += (int)(c - 'A' + 1) * (int)Math.pow(26, length - i - 1);
continue;
}
return 0;
}
return result;
}public String I2S(int i) {
if (i < 1)
return "";
int temp = i;
StringBuffer sb = new StringBuffer();
while (temp != 0) {
int reminder = temp % 26;
if (reminder == 0) {
reminder = 26;
temp--;
}
sb.append((char)(reminder + 'A' - 1));
temp /= 26;
}
return sb.reverse().toString();
}hmmm,一组互转的函数
2<->B
.....a%26 取余数。然后去hash里取我只先想到这个了。。
public class LowToUpper {
public static void main(String args[]){
char[] charArray = new char[27];
char temp = 'A';
for(int i=1; i<charArray.length; i++){//避免麻烦从1开始
charArray[i] = temp;
temp ++;
}
int[] intArray = {1, 17, 27, 127, 2397};
for(int i=0; i<intArray.length; i++){
if(intArray[i] < 26)
System.out.println(charArray[intArray[i]]);
else{
int first = intArray[i]/26;
int second = intArray[i]%26;
while(first > 26){
first = first/26;
second = first%26;
System.out.print(charArray[first]);
}
System.out.print(charArray[first]);
System.out.print(charArray[second] +"\n");
}
}
}
}
输出:
A
Q
AA
DW
CCC
额,2397的结果我算出来不太一样额………………
我和大虫前辈算出来都是CNE
public class LowToUpper {
public static void main(String args[]){
char[] charArray = new char[27];
char temp = 'A';
for(int i=1; i<charArray.length; i++){//避免麻烦从1开始
charArray[i] = temp;
temp ++;
}
int[] intArray = {1, 17, 27, 127, 2397, 12335000};
for(int i=0; i<intArray.length; i++){
StringBuffer sb = new StringBuffer();
if(intArray[i] < 26)
System.out.println(charArray[intArray[i]]);
else{
int first = intArray[i]/26;
int second;
int third = intArray[i]%26;
sb.append(charArray[third]); while(first > 26){
second = first%26;
first = first/26; sb.append(charArray[second]);
}
sb.append(charArray[first]);
System.out.println(sb.reverse().toString());
}
}
}
}
输出:
A
Q
AA
DW
CNE
ZYUAB
public class T { public static char get(int i) {
if (i > 26 || i < 1) {
throw new RuntimeException("请输入1-26中任意一个整数");
}
return (char) (i + 64);
} public static void main(String[] args) {
System.out.println(get(1));
}
}