Java,替换for循环中的字符

时间:2014-10-20 01:33:42

标签: java arrays encryption replace char

好的,显然我是Java的新手,而我目前希望做的是一个非常简单的程序,通过将字符串分成字符数组并用新字符替换字符来加密字符串。

所以我到目前为止所做的是创建一个包含字母表的键阵列,我将比较分裂字符串,我试图用值数组替换字符这基本上只是向后的字母表。

到目前为止,我的代码在我打印出值时起作用,但它不能正确替换字符。

public class Main {

    public static void main(String[] args) {

        char[] keyArray =  {'a', 'b', 'c', 'd',  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char[] valueArray = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'p', 'q', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd',  'c', 'b', 'a'};

        String myString = "abcxyz";
        char[] myStringArray = myString.toCharArray();

        for(int x = 0; x<myString.length(); x++)
        {
            for(int i = 0; i<keyArray.length; i++)
            {
                if(myStringArray[x] == keyArray[i])
                {
                    //System.out.println(valueArray[i]); would give the output "zyxcba" as expected 
                    myStringArray[x] = valueArray[i]; // this will only change the characters in the first half of keyArray 
                }
            }
        }

        System.out.println(myStringArray); //Outputs "abccba" instead of "zyxcba"
    }
}

2 个答案:

答案 0 :(得分:3)

你遇到的问题是,即使你已经进行了替换,你仍然会继续循环关键阵列 - 允许它再次替换它!

你需要打破&#39;一旦你完成了替换,你就会离开for循环。

public class Main {

    public static void main(String[] args) {

        char[] keyArray =  {'a', 'b', 'c', 'd',  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        char[] valueArray = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'p', 'q', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd',  'c', 'b', 'a'};

        String myString = "abcxyz";
        char[] myStringArray = myString.toCharArray();

        for(int x = 0; x<myString.length(); x++)
        {
            for(int i = 0; i<keyArray.length; i++)
            {
                if(myStringArray[x] == keyArray[i])
                {
                    //System.out.println(valueArray[i]); would give the output "zyxcba" as expected 
                    myStringArray[x] = valueArray[i]; // this will only change the characters in the first half of keyArray 
                    break; //Exit the loop checking against the keyArray
                }
            }
        }

        System.out.println(myStringArray); //Outputs "abccba" instead of "zyxcba"
    }
}

答案 1 :(得分:0)

public class Main {

public static void main(String[] args) {

    char[] keyArray =  {'a', 'b', 'c', 'd',  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    char[] valueArray = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'p', 'q', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd',  'c', 'b', 'a'};

    String myString = "abcxyz";
    char[] myStringArray = myString.toCharArray();

    for(int x = 0; x<myString.length(); x++)
    {
        for(int i = 0; i<keyArray.length; i++)
        {
            if(myStringArray[x] == keyArray[i])
            {
                //System.out.println(valueArray[i]); would give the output "zyxcba" as expected 
                myStringArray[x] = valueArray[i]; // this will only change the characters in the first half of keyArray
                break; //Introduced break so that when the answer is set, break and move to the next iteration of myStringArray
            }
        }
    }


    System.out.println(myStringArray); //Outputs "abccba" instead of "zyxcba"
}

}

我介绍了介于两者之间的中断以控制项目匹配的时间。

尽管如此,还有许多其他方法可以实现这一目标。但主要的是你不应该在不需要时进行x * i比较。