2013-02-13 70 views
-2

我有一个要求,在给定字母和数字时需要返回字母表。如果给定,C和4 I将返回C + 4 = G 同样如果给出C和-2,我将返回C +( - 2)= A如何获得alphabates字母数字并对其进行操作

如果我有AA那么AA + 4 = AD所以我会一直想从字符串中取出最后一个字符。

我想使用字符串数组来存储字母,但它似乎有点不好的解决方案。有什么办法可以让它做得更好?

+1

这个不清楚。你是说你想得到一些函数'foo(char c,int i)',它返回''G''foo('C',4)'?如果是这样,那只是'c + i'。 – 2013-02-13 21:34:35

+1

'A'和'-2'的结果是什么? – Pshemo 2013-02-13 21:35:37

+0

看起来你不需要存储整个字母表,但只有一个字母?!数学表达式可以比' + '更复杂吗?一般来说,因为你想做数学,你必须确保你的信件最终被转换为数字,否则你将无法添加它们。 – Thomas 2013-02-13 21:35:52

回答

2

字母字符都已经按顺序排列,所有您需要做的就是向其中一个添加数字。

我想你想是这样的:

addToChar('A', 4); 

char addToChar(char inChar, int inNum) 
{ 
    return (char)(inChar + inNum); 
} 

您可能要检查它是否是比“Z”以及低于“A”或更大。

在回答您的编辑:

void addToChar(char[] inChars, int inNum) 
{ 
    for (int i = inChars.length-1; inNum != 0 && i >= 0; i--) 
    { 
     int result = inChars[i]-'A'+inNum; 
     if (result >= 0) 
     { 
     inNum = result/26; 
     result %= 26; 
     } 
     else 
     { 
     inNum = 0; 
     while (result < 0) // there may be some room for optimization here 
     { 
      result += 26; 
      inNum--; 
     } 
     } 
     inChars[i] = (char)('A'+result); 
    } 
} 

为了应对溢出:(有点低效率)('Z' + 1输出'AA'

static String addToChar(String inChars, int inNum) 
{ 
    String output = ""; 
    for (int i = inChars.length()-1; inNum != 0 || i >= 0; i--) 
    { 
     if (i < 0 && inNum < 0) 
     return "Invalid input"; 
     int result = i >= 0 ? inChars.charAt(i)-'A'+inNum 
          : -1+inNum; 
     if (result > 0) 
     { 
     inNum = result/26; 
     result %= 26; 
     } 
     else 
     { 
     inNum = 0; 
     while (result < 0) 
     { 
      result += 26; 
      inNum--; 
     } 
     } 
     output = (char)('A'+result) + output; 
    } 
    return output; 
} 
+0

我编辑了问题 – AnujKu 2013-02-13 21:52:51

+0

@WarriorPrince再次编辑(以正确处理负面输入)。 – Dukeling 2013-02-13 22:09:18

+0

@WarriorPrince再次编辑(处理溢出)。 – Dukeling 2013-02-13 22:30:32

0

您不需要在字符串中存储字母;这是ASCII为什么连续排列所有字母的原因之一。

执行数学运算,将数学运算隐式转换为int,然后将结果转换为char。你必须检查你是否在'A'之前或'Z'之后。

这是ASCII table reference

+0

你是对的;我编辑了我的答案。 – rgettman 2013-02-13 21:41:12

+0

包含[Unicode参考](http://en.wikibooks.org/wiki/Unicode/Character_reference/0000-0FFF)可能更为正确,因为这是Java使用的。 – Dukeling 2013-02-13 21:48:13

1

尝试这个,例如:

public class example { 

public static void main(String[] args) { 

    int number = 2; 
    char example = 'c'; 

    System.out.println((char)(example+number)); 

    } 
} 
+0

再次检查问题。编辑 – AnujKu 2013-02-13 21:54:06

+0

我添加了另一个更新与新问题的答案 – Nimrod007 2013-02-13 22:07:28

0

你有没有谷歌关于字符集?与ASCII一样,一个字符已经由一个数字表示。

0

首先,将您的角色转换为int,然后添加您的int,并将其转换回char。例如:

char c = 'c'; 
int cInt = (int)c; 
int gInt = cInt + 4; 
char g = (char)gInt; // 'G' 
1

这是更新的问题的例子:

仍然需要验证输入数量和输入的字符串(可以说,如果数字是124发生了什么?)

public class example { 

public static void main(String[] args) { 

    int number = 1; 
    String example = "nicd"; 
    //get the last letter from the string 
    char lastChar = example.charAt(example.length()-1); 
    //add the number to the last char and save it 
    lastChar = (char) (lastChar+number); 
    //remove the last letter from the string 
    example = example.substring(0, example.length()-1); 
    //add the new letter to the end of the string 
    example = example.concat(String.valueOf(lastChar)); 
    //will print nice 
    System.out.println(example); 

    } 
} 
相关问题