【问题标题】:How to get alphabates letter number and operate on it [closed]如何获取字母数字并对其进行操作[关闭]
【发布时间】:2013-02-13 21:32:37
【问题描述】:

我有一个要求,我需要在给定字母和数字时返回字母。 如果给出示例,C 和 4 I 将返回 C+4 = G 此外,如果给定 C 和 -2 我将返回 C + (-2) = A

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

我正在考虑使用字符串数组来存储字母,但这似乎是一种糟糕的解决方案。有什么办法可以让我做得更好吗?

【问题讨论】:

  • 这不清楚。你是说你想得到一些函数foo(char c, int i),它为foo('C', 4)返回'G'?如果是这样,那就是c + i
  • A-2 的结果是什么?
  • 看起来您不需要存储整个字母表,而只需要存储一个字母?!数学表达式能比<letter> + <number>更复杂吗?一般来说,既然你想做数学,你必须确保你的字母最终被转换成数字,否则你将无法添加它们。
  • 请大家再次检查问题。在投票之前。
  • AZ+1 的结果是什么?是BAAA 还是别的什么(例如例外)?

标签: java arrays string algorithm


【解决方案1】:

字母字符都已经排列好了,你需要做的就是给一个加一个数字来得到另一个。

我想你想要这样的东西:

addToChar('A', 4);

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

您可能还想检查它是小于“A”还是大于“Z”。

回应您的修改:

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

【讨论】:

  • 我已经编辑了问题
  • @WarriorPrince 再次编辑(正确处理负面输入)。
  • @WarriorPrince 再次编辑(处理溢出)。
【解决方案2】:

例如试试这个:

public class example {

 public static void main(String[] args) {

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

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

    }
 }

【讨论】:

  • 再次检查问题。已编辑
  • 我添加了另一个更新了新问题的答案
【解决方案3】:

这是更新问题的示例:

仍然需要验证输入的数字和输入的字符串(让我们说如果数字是 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);

    }
 }

【讨论】:

    【解决方案4】:

    你曾经在谷歌上搜索过字符集吗?与 ASCII 一样,字符已经由数字表示。

    【讨论】:

      【解决方案5】:

      您不需要将字母存储在数组中;这就是 ASCII 的所有字母都按连续顺序排列的原因之一。

      执行数学运算,将char 隐式转换为int,然后将结果转换为char。你必须检查你没有在“A”之前或“Z”之后去。

      这是ASCII table reference

      【讨论】:

      • 你是对的;我已经编辑了我的答案。
      • 包含Unicode reference 可能更正确,因为这是 Java 使用的。
      【解决方案6】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-01
        • 1970-01-01
        • 2020-04-18
        相关资源
        最近更新 更多