1
public static String encrypt (String h, int a) throws Exception
{
  h = h.toLowerCase();

  if(h.contains("a"))
  {
     data = data.replace("a", "c");
  }
  if(h.contains("b"))
  {
     data = data.replace("b", "d");
  }
  if(h.contains("c"))
  {
     data = data.replace("c", "e");
  }
  if(h.contains("d"))
  {
     data = data.replace("d", "f");
  }
  if(h.contains("e"))
  {
     data = data.replace("e", "g");
  }
  if(h.contains("f"))
  {
     data = data.replace("f", "h");
  }
  if(h.contains("g"))
  {
     data = data.replace("g", "i");
  }
  if(h.contains("h"))
  {
     data = data.replace("h", "j");
  }
  if(h.contains("i"))
  {
     data = data.replace("i", "k");
  }
  if(h.contains("j"))
  {
     data = data.replace("j", "l");
  }
  if(h.contains("k"))
  {
     data = data.replace("k", "m");
  }
  if(h.contains("l"))
  {
     data = data.replace("l", "n");
  }
  if(h.contains("m"))
  {
     data = data.replace("m", "o");
  }
  if(h.contains("n"))
  {
     data = data.replace("n", "p");
  }
   if(h.contains("o"))
  {
     data = data.replace("o", "q");
  }
   if(h.contains("p"))
  {
     data = data.replace("p", "r");
  }
   if(h.contains("q"))
  {
     data = data.replace("q", "s");
  }
   if(h.contains("r"))
  {
     data = data.replace("r", "t");
  }
   if(h.contains("s"))
  {
     data = data.replace("s", "u");
  }
   if(h.contains("t"))
  {
     data = data.replace("t", "v");
  }
   if(h.contains("u"))
  {
     data = data.replace("u", "w");
  }
   if(h.contains("v"))
  {
     data = data.replace("v", "x");
  }
   if(h.contains("w"))
  {
     data = data.replace("w", "y");
  }
   if(h.contains("x"))
  {
     data = data.replace("x", "z");
  }
   if(h.contains("y"))
  {
     data = data.replace("y", "a");
  }
   if(h.contains("z"))
  {
     data = data.replace("z", "b");
  }

  return h;
}      

当我运行它时,它不会替换正确的字母。我不知道如何解决这个问题,只是在寻求帮助。

4

2 回答 2

2

加密字符串的方式使字母被替换不止一次。如果你真的想使用'if'语句,你可以这样做:

static String encrypt (String data)
{
    String returned = new String ("");

    for (char h: data.toCharArray())
    {
        if(h == 'a')
            returned += " a', 'c";
        if(h == 'b')
            returned += "d";
        if(h == 'c')
            returned += "e";
        if(h == 'd')
            returned += "f";
        if(h == 'e')
            returned += "g";
        if(h == 'f')
            returned += "h";
        if(h == 'g')
            returned += "i";
        if(h == 'h')
            returned += "j";
        if(h == 'i')
            returned += "k";
        if(h == 'j')
            returned += "l";
        if(h == 'k')
            returned += "m";
        if(h == 'l')
            returned += "n";
        if(h == 'm')
            returned += "o";
        if(h == 'n')
            returned += "p";
        if(h == 'o')
            returned += "q";
        if(h == 'p')
            returned += "r";
        if(h == 'q')
            returned += "s";
        if(h == 'r')
            returned += "t";
        if(h == 's')
            returned += "u";
        if(h == 't')
            returned += "v";
        if(h == 'u')
            returned += "w";
        if(h == 'v')
            returned += "x";
        if(h == 'w')
            returned += "y";
        if(h == 'x')
            returned += "z";
        if(h == 'y')
            returned += "a";
        if(h == 'z')
            returned += "b";
    }
    return returned;
}

但这种算法最好使用哈希表。

于 2015-03-24T18:21:30.767 回答
2

这是执行此特定加密的一种更简单的方法:

static String encrypt (String data)
{
    String encrypted = new String ("");

    for (char c : data.toCharArray())
    {
        char newC = c+2;
        if(newC > 'z')
            newC -= 26;

        encrypted += newC;
    }

    return encrpyted;
}
于 2015-03-24T19:02:25.007 回答