Java,替换for循环中的字符

问题描述:

好吧,显然,我是Java新手,我目前想做的是一个非常简单的程序,通过将字符串拆分为字符数组并替换字符来加密字符串与新的。Java,替换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 
       } 
      } 
     } 

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

这可能有所帮助:http://stackoverflow.com/q/11588916/535275 – 2014-10-20 01:38:12

你的问题是,你继续遍历数组键即使你已经做了替换 - 允许它来代替它第二次!

一旦您完成替换,您需要从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" 
    } 
} 
+0

啊,我明白!非常感谢你,一切都按预期工作:) – s1nclair 2014-10-20 01:41:31

公共类主要{

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 *我比较,当它不是必需的。

+0

我还会添加一件事情,不是将字符串转换为数组,而是使用字符串方法charAt或substring来执行操作。 – scorpionmance 2014-10-20 02:14:59

+0

谢谢你的回答!是的,我还没有探索这样做的各种选择,这是我的第一个Java程序。我会仔细看看字符串操作!但是,您对x * i的比较意味着什么?你是说我应该使用别的东西而不是for-loops? – s1nclair 2014-10-20 09:23:28

+0

通过x *我,我的意思是这种情况下的总数将是myString.length()* keyArray.length()这不是一个有效的方式来解决问题,在这种情况下,你很好地获得你的结果在内部循环结束之前。 – scorpionmance 2014-10-21 03:22:45