2014-10-20 127 views
-1

好吧,顯然,我是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

回答

3

你的問題是,你繼續遍歷數組鍵即使你已經做了替換 - 允許它來代替它第二次!

一旦您完成替換,您需要從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

0

公共類主要{

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