在Java中添加两个字符数组并将结果作为字符存储在另一个字符数组中

在Java中添加两个字符数组并将结果作为字符存储在另一个字符数组中,java,arrays,ascii,modulo,Java,Arrays,Ascii,Modulo,我想添加两个字符数组,并将结果作为字符存储在另一个字符数组中。 加法包括将两个数组中的两个英文字母作为操作数相加。 加法将围绕z回到a 例如。 输入:数组1和数组2,输出为数组3 数组1:abcdeyz 阵列2:aaaaaaaaa 数组3:bcdefza 下面是我的代码中不起作用的一部分。 请提出任何修改意见 int c = 0; char array3[] = new char[count] ; for(int a=0;a<array1.length;a++)

我想添加两个字符数组,并将结果作为字符存储在另一个字符数组中。 加法包括将两个数组中的两个英文字母作为操作数相加。 加法将围绕z回到a

例如。 输入:数组1和数组2,输出为数组3

数组1:abcdeyz

阵列2:aaaaaaaaa

数组3:bcdefza

下面是我的代码中不起作用的一部分。 请提出任何修改意见

    int c = 0;

    char array3[] = new char[count] ;    

for(int a=0;a<array1.length;a++)
    {
        for(int b=0;b<array2.length;b++)
        {
            int temp = (array1[a] + array2[b]) % 26 ;
            array3[c] = (char) temp ; 
            c++ ;
        }
    }
intc=0;
字符数组3[]=新字符[计数];

对于(int a=0;aHint-“a”的值不是0

问题就在眼前-

int temp = (array1[a] + array2[b]) % 26 ;
这是修改后的代码(假设所有字符都是小写)-

intc=0;
字符数组3[]=新字符[计数];
对于(int a=0;a写下:

int temp = (((array1[a] - 'a') + (array2[b] - 'a')) % 26) + 'a';
这样做的目的是将两个字符转换为它们在字母表中各自的位置,对字母表中的字符数执行加法模运算以获得环绕效果,然后转换回正确的ascii值


请注意,您的代码遇到了问题,因为您的行为好像字母表的ascii值是字母表本身中各自的位置,这是不正确的。

这是一个关于如何执行此操作的固定且有效的示例:

public static void main(String[] args) {
    char[] array1 = new char[] {'a', 'b', 'c', 'd', 'e', 'y', 'z'};
    char[] array2 = new char[] {'a', 'a', 'a', 'a', 'a', 'a', 'a'};
    char[] array3 = new char[array1.length];

    for (int i = 0; i < array1.length; i++) {
      array3[i] = toChar((toInt(array1[i]) + toInt(array2[i]) + 1) % 26);
    }
    System.out.println(Arrays.toString(array3));
}

private static int toInt(char chr) {
    return chr - 'a';
}

private static char toChar(int value) {
    return (char)(value + 'a');
}
publicstaticvoidmain(字符串[]args){
char[]array1=新字符[]{'a','b','c','d','e','y','z'};
char[]array2=新字符[]{'a','a','a','a','a','a','a'};
char[]array3=新字符[array1.length];
for(int i=0;i
这里有一些想法需要注意(除了
-'a'
部分,其他答案已经提到):

  • 此任务只需要一个循环。如果使用两个嵌套循环,则会将
    array1
    的每个字母与
    array2
    的每个字母相加。并且会有一个更大的
    array3
    。由于结果与所需结果不匹配…:)

  • toInt(array2[i])+1
    中的
    +1
    是必需的,因为
    字符-a'
    是“零基的”。因此
    'a'+'b'
    将导致
    'b'
    而不是
    'c'
    ,因为你将计算
    0+1
    (如果你“转换”,它将是
    'b'
    )通过添加
    'a'
    )返回到char(我希望这是可以理解的:D)

  • array2
    必须至少具有与“array1”相同的长度。这几乎与相同。(我省略了填充部分以保持此代码的简短)


使用26模是处理a-z到1-26转换的好方法。这样,您的程序就可以不知道实际的ASCII字符数

其他一些关切:

  • 不区分大小写。我建议在处理信件之前将其转换为小写
  • 异常情况的处理。如果数组长度不同怎么办?或者如果数组中的字符不是 信
  • 下面的代码是处理这些事情的一种方法

    public static int letterToInt(char letter) {
        char letterToConvert = Character.toLowerCase(letter);
        int codeForA = (int)'a';
        int numberOfLetter = ((int)letterToConvert) - codeForA + 1;
        if(numberOfLetter < 1 || numberOfLetter > 26) {
            throw new IllegalArgumentException(
                    "The character argument can only be a-z or A-Z, but was '" + letter + "'");
        }
        return numberOfLetter;
    }
    
    public static char intToLetter(int number) {
        if(number < 1 || number > 26) {
            throw new IllegalArgumentException(
                    "The number can only be 1-26, but was " + number);
        }
        int codeForA = (int)'a';
        return (char)(codeForA + number - 1);
    }
    
    public static char addLetters(char letter1, char letter2) {
        int numberFromAddedLetters =
                letterToInt(letter1) + letterToInt(letter2);
        int modulo = numberFromAddedLetters % 26;
        return intToLetter(modulo == 0 ? 26 : modulo);
    }
    
    public static char[] addLetterArrays(char[] array1, char[] array2) {
        char[] longerArray;
        char[] shorterArray;
        if(array1.length >= array2.length) {
            longerArray = array1;
            shorterArray = array2;
        } else {
            longerArray = array2;
            shorterArray = array1;
        }
    
        char[] addedLetters = new char[longerArray.length];
        for(int index = 0; index < longerArray.length; index++) {
            if(index < shorterArray.length) {
                addedLetters[index] = addLetters(longerArray[index], shorterArray[index]);
            } else {
                addedLetters[index] = longerArray[index];
            }
        }
        return addedLetters;
    }
    
    // Test it out
    public static void main(String[] args) {
        char[] letters1 = "abcdeyz".toCharArray();
        char[] letters2 = "aaaaaaa".toCharArray();
        // Prints [b, c, d, e, f, z, a]
        System.out.println(Arrays.toString(addLetterArrays(letters1, letters2)));
    }
    
    public static int letterToInt(字符字母){
    char letterToConvert=字符.toLowerCase(字母);
    int codeForA=(int)“a”;
    int numberOfLetter=((int)letterToConvert)-codeForA+1;
    如果(numberOfLetter<1 | | numberOfLetter>26){
    抛出新的IllegalArgumentException(
    字符参数只能是a-z或a-z,但为“+”字母+”;
    }
    回信号码;
    }
    公共静态字符intToLetter(整数){
    如果(编号<1 | |编号>26){
    抛出新的IllegalArgumentException(
    “编号只能为1-26,但为”+编号);
    }
    int codeForA=(int)“a”;
    返回(字符)(codeForA+数字-1);
    }
    公共静态字符addLetters(字符letter1,字符letter2){
    int numberFlomaddletters=
    字体(字体1)+字体(字体2);
    整数模=numberFlomaddletters%26;
    返回整数(模==0?26:模);
    }
    公共静态字符[]addletterarray(字符[]数组1,字符[]数组2){
    字符[]长阵列;
    字符[]短排列;
    if(array1.length>=array2.length){
    长阵列=阵列1;
    短阵列=阵列2;
    }否则{
    长阵列=阵列2;
    短阵列=阵列1;
    }
    char[]addedLetters=新字符[longerArray.length];
    对于(int index=0;index
    那么把ascii值加起来呢?@jgr208不,它更像是,但具有可变范围。这不会将两个数组相加together@Tom是,但根据2个阵列的输入,每个字符都会被不同的字符替换。为什么要使用2个嵌套循环?循环只需要一个
    ,如果“键”如果array1[a]='x'和array2[b]='z'?我们需要将%26保持在[0,26]的范围内,根据OP的声明“加法将把z绕回a”你能帮我一个忙吗?试试这段代码,看看你是否得到了所需的array3输出。我想,首先,你会得到一个
    ArrayIndexOutOfBoundsException
    .De
    public static int letterToInt(char letter) {
        char letterToConvert = Character.toLowerCase(letter);
        int codeForA = (int)'a';
        int numberOfLetter = ((int)letterToConvert) - codeForA + 1;
        if(numberOfLetter < 1 || numberOfLetter > 26) {
            throw new IllegalArgumentException(
                    "The character argument can only be a-z or A-Z, but was '" + letter + "'");
        }
        return numberOfLetter;
    }
    
    public static char intToLetter(int number) {
        if(number < 1 || number > 26) {
            throw new IllegalArgumentException(
                    "The number can only be 1-26, but was " + number);
        }
        int codeForA = (int)'a';
        return (char)(codeForA + number - 1);
    }
    
    public static char addLetters(char letter1, char letter2) {
        int numberFromAddedLetters =
                letterToInt(letter1) + letterToInt(letter2);
        int modulo = numberFromAddedLetters % 26;
        return intToLetter(modulo == 0 ? 26 : modulo);
    }
    
    public static char[] addLetterArrays(char[] array1, char[] array2) {
        char[] longerArray;
        char[] shorterArray;
        if(array1.length >= array2.length) {
            longerArray = array1;
            shorterArray = array2;
        } else {
            longerArray = array2;
            shorterArray = array1;
        }
    
        char[] addedLetters = new char[longerArray.length];
        for(int index = 0; index < longerArray.length; index++) {
            if(index < shorterArray.length) {
                addedLetters[index] = addLetters(longerArray[index], shorterArray[index]);
            } else {
                addedLetters[index] = longerArray[index];
            }
        }
        return addedLetters;
    }
    
    // Test it out
    public static void main(String[] args) {
        char[] letters1 = "abcdeyz".toCharArray();
        char[] letters2 = "aaaaaaa".toCharArray();
        // Prints [b, c, d, e, f, z, a]
        System.out.println(Arrays.toString(addLetterArrays(letters1, letters2)));
    }