如何将2添加到每个char的unicode值。它只会改变第一个字符。输入:ab。输出:cb

时间:2015-01-07 23:52:11

标签: java unicode

import java.util.Scanner;

public class TESTING45363

{

    public static void main(String[] arg)

    {

    String word;  
      int index = 0;
      char [] word2;
      System.out.print("Input Letter: ");
      Scanner input = new Scanner (System.in);
      word = input.nextLine();
      word2 = word.toCharArray();
      input.close();

      for (int i = 0; i <= word2.length; i++);

      {

         switch (word2[index])      

          {

            case 'a': word2[index] = 97 + 2; break;
            case 'b': word2[index] = 98 + 2; break;
            case 'c': word2[index] = 99 + 2; break;
            case 'd': word2[index] = 100 + 2; break;
            case 'e': word2[index] = 101 + 2; break;
            case 'f': word2[index] = 102 + 2; break;
            case 'g': word2[index] = 103 + 2; break;
            case 'h': word2[index] = 104 + 2; break;
            case 'i': word2[index] = 105 + 2; break;
            case 'j': word2[index] = 106 + 2; break;
            case 'k': word2[index] = 107 + 2; break;
            case 'l': word2[index] = 108 + 2; break;
            case 'm': word2[index] = 109 + 2; break;
            case 'n': word2[index] = 110 + 2; break;
            case 'o': word2[index] = 111 + 2; break;
            case 'p': word2[index] = 112 + 2; break;
            case 'q': word2[index] = 113 + 2; break;
            case 'r': word2[index] = 114 + 2; break;
            case 's': word2[index] = 115 + 2; break;
            case 't': word2[index] = 116 + 2; break;
            case 'u': word2[index] = 117 + 2; break;
            case 'v': word2[index] = 118 + 2; break;
            case 'w': word2[index] = 119 + 2; break;
            case 'x': word2[index] = 120 + 2; break;
            case 'y': word2[index] = 121 - 24; break;
            case 'z': word2[index] = 122 - 24; break;

         }

         index = index + 1;

      } 

      String newString1 = new String(word2);
      System.out.println("New word: " + newString1);

   }

} 

如何将2添加到每个char的unicode值。它只会改变第一个字符。输入:ab。输出:cb! Plz帮助。到目前为止,我没有运气。

1 个答案:

答案 0 :(得分:1)

首先,在for循环语句的末尾删除该分号。它充当主体,后面的块只执行一次。

接下来,在到达word2.length之前停止循环; Java索引从0length - 1

for (int i = 0; i < word2.length; i++)