3

如何通过java中的数字位置获取字母?

假设我输入了 1 作为输出,我需要得到 A 如何在 java 中获得字母位置?

提前致谢。

4

4 回答 4

7

试试这个

int i = 1;
System.out.println((char)(i+'A'-1));
于 2012-10-17T04:24:59.973 回答
2
int charValue = 1; //this is the number you enter
char letter = (char)(charValue+64);  //this is the character you want

对于小写字母,使用 (charValue+96)

于 2012-10-17T04:25:32.530 回答
0

您可以使用switch/case 语句手动获取每个字母,但是更好的解决方案是使用 ASCII 表来获取字母。

ASCII 表:http ://www.ascii-code.com/

public char getLetter(int i)
{
  return (char) (i + 64);
}

当 i 为 1 时,上述函数将返回 'A'

于 2012-10-17T04:24:55.907 回答
0

您有字母/字符,将它们视为数组中的位置。

int number = 0;
String[] array = new String[] {"a", "b", "c", "..."};
String letter = array[number + 1];
于 2012-10-17T04:25:16.560 回答