更简便的方式将索引(0-7)翻译为字母(A-H)。

huangapple go评论102阅读模式
英文:

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));
}

huangapple
  • 本文由 发表于 2020年10月3日 14:56:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64181556.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定