如何向每个字符的unicode值中添加2。它只更改第一个字符。。例输入:ab输出:cb import java.util.Scanner; 公开课考试45363 { 公共静态void main(字符串[]arg) { 字符串字; int指数=0; char[]word2; 系统输出打印(“输入字母:”); 扫描仪输入=新扫描仪(System.in); word=input.nextLine(); word2=word.toCharArray(); input.close(); 对于(int i=0;i

如何向每个字符的unicode值中添加2。它只更改第一个字符。。例输入:ab输出:cb import java.util.Scanner; 公开课考试45363 { 公共静态void main(字符串[]arg) { 字符串字; int指数=0; char[]word2; 系统输出打印(“输入字母:”); 扫描仪输入=新扫描仪(System.in); word=input.nextLine(); word2=word.toCharArray(); input.close(); 对于(int i=0;i,java,unicode,Java,Unicode,首先,删除for循环语句末尾的分号。它充当主体,后面的块只执行一次 接下来,在到达word2.length之前停止循环;Java索引从0运行到length-1 import java.util.Scanner; public class TESTING45363 { public static void main(String[] arg) { String word; int index = 0; char [] word2;

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

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

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

   }

} 
for(int i=0;i
我相信您可以想出一种更有效的方法来编写
开关
语句。您有什么建议吗?
if(word2[index]>='a'&&word2[index]您也不需要同时使用
i
index
——现在,它们在整个循环中都是相等的。我的for循环中会有if语句吗?它在线程“main”中表示异常java.lang.ArrayIndexOutOfBoundsException:-1当我在答案中进行更改时,它对我有效。
wxyz
->
yzab
。我没有添加任何内容。我将您的
for
循环行更改为答案中的代码。除了删除分号并更改这些是我唯一做的更改。首先进行更改,然后继续在这之后,对于更干净的代码,您可以按照@DavidWallace的建议替换
switch
语句。
for (int i = 0; i < word2.length; i++)