英文:
Easier way to translate index (0-7) to letters (A-H)
问题
这种方法正在处理工作,但我确信一定有更好的方法可以做到这一点...?
private String posLetter(int posX) {
String letter = "";
if (posX == 0) {letter = "A";}
if (posX == 1) {letter = "B";}
if (posX == 2) {letter = "C";}
if (posX == 3) {letter = "D";}
if (posX == 4) {letter = "E";}
if (posX == 5) {letter = "F";}
if (posX == 6) {letter = "G";}
if (posX == 7) {letter = "H";}
return letter;
}
英文:
This method is handling the job, but I am sure there must be a better way to do this...?
private String posLetter(int posX) {
String letter = "";
if (posX == 0) {letter = "A";}
if (posX == 1) {letter = "B";}
if (posX == 2) {letter = "C";}
if (posX == 3) {letter = "D";}
if (posX == 4) {letter = "E";}
if (posX == 5) {letter = "F";}
if (posX == 6) {letter = "G";}
if (posX == 7) {letter = "H";}
return letter;
}
答案1
得分: 2
如何?
String [] arr = {"A", "B", ....}
return arr[posX]
英文:
How about?
String [] arr = {"A", "B", ....}
return arr[posX]
答案2
得分: 2
以下是翻译好的内容:
并利用ASCII码:
private String posLetter(int posX) {
return String.valueOf((char) (65 + posX));
}
英文:
And to exploit ASCII codes:
private String posLetter(int posX) {
return String.valueOf((char) (65 + posX));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论