2012-02-18 148 views
1

你好,我有這個代碼我正在工作,基本上這將是一個遊戲,你必須破解一個句子的形式的代碼。替換字符串中的所有字符不同的字符

我遇到麻煩的是用代碼中的字符替換句子中的字符。

你替換字符代碼的一個例子是這樣的 關鍵字老虎 TIGERABCDFHJKLMNOPQSUVWXYZ

基本字母變成了關鍵字,則未使用的字母 我試圖改變一個句子中其餘字符與編碼的字母,如果這是有道理的。

但是,當我嘗試多次替換字符它不正確地更改字母。

我需要這部分的幫助才能繼續遊戲的其餘部分。

import javax.swing.JOptionPane; 

public class CodeCrackerGame 
/********************************************************* 
* Code Cracker Game 
*********************************************************/ 
{ 
    public static void main(String[] args) 
    { 

    /******************************************** 
    * 1st Player's SENTENCE 
    *******************************************/ 
    String sentence = JOptionPane.showInputDialog("Enter sentence to be" 
     + "encrypted." + "\nThis sentence can be up to 100 characters," 
     + "\nand has a minimum of 7 words."); 

    /*Check if hit cancel or x button*/ 
    if(sentence == null) 
    { 
     JOptionPane.showMessageDialog(null, "No input", "ERROR", 
      JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 
    /*Checks if just hit OK*/ 
    else if(sentence.equals("")) 
    { 
     JOptionPane.showMessageDialog(null, "No Input", "ERROR", 
      JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 

    /*Check if over 100 characters*/ 
    if(sentence.length() > 100) 
    { 
     JOptionPane.showMessageDialog(null, "Input has too many characters", 
      "ERROR", JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 

    int words = 1; 
    int i; 
    /*Counts spaces to find amount of words*/ 
    for (i= 0; i< sentence.length(); i++) 
    { 
     if(sentence.charAt(i)== ' ') 
     { 
     words += 1; 
     } 
    } 
    /*Checks if there are less than 7 words*/ 
    if(words < 7) 
    { 
     JOptionPane.showMessageDialog(null, "Not enough words", 
      "ERROR", JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 

    sentence = sentence.toUpperCase(); 
    /*Check if not alphabetic and if contains illegal punctuation*/ 
    for (i= 0; i< sentence.length(); i++) 
    { 
     if(Character.isLetter(sentence.charAt(i)) == false && 
     sentence.charAt(i)!= '!' && sentence.charAt(i)!= ' ' && 
     sentence.charAt(i)!= ',' && sentence.charAt(i)!= '.' && 
     sentence.charAt(i)!= '?' && sentence.charAt(i)!= '-' && 
     sentence.charAt(i)!= ':' && sentence.charAt(i)!= ';' && 
     sentence.charAt(i)!= '(' && sentence.charAt(i)!= ')' && 
     sentence.charAt(i)!= '[' && sentence.charAt(i)!= ']' && 
     sentence.charAt(i)!= '/' && sentence.charAt(i)!= '\'' && 
     sentence.charAt(i)!= '"') 
     { 
      JOptionPane.showMessageDialog(null, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
       + "! , . ? - : ; () [ ]/' \" are allowed", 
       "ERROR", JOptionPane.ERROR_MESSAGE); 
      System.exit(0); 
     } 
    } 

    /*Checks if string ends with . ! ?*/ 
    char c = sentence.charAt(sentence.length()-1); 
    if(c != '.' && c != '!' && c != '?') 
    { 
     JOptionPane.showMessageDialog(null, "Must end with '.' '!' '?'", 
      "ERROR", JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 
    /*prints out the sentence*/ 
    JOptionPane.showMessageDialog(null, sentence, 
     "Your message in all CAPS", JOptionPane.INFORMATION_MESSAGE); 

    /************************************************** 
    * 1st Player's Keyword 
    **************************************************/ 

    String keyword = JOptionPane.showInputDialog("Enter a keyword" 
     + "\nwith up to 10 characters"); 
    /*Check if hit cancel or x button*/ 
    if(keyword == null) 
    { 
     JOptionPane.showMessageDialog(null, "No input", "ERROR", 
       JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 
    /*Checks if just hit OK*/ 
    else if(keyword.equals("")) 
    { 
     JOptionPane.showMessageDialog(null, "No Input", "ERROR", 
      JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 

    /*Check if keyword is greater than 10 characters*/ 
    if(keyword.length() > 10) 
    { 
     JOptionPane.showMessageDialog(null, "Keyword has too many characters", 
      "ERROR", JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 
    /*Check if keyword is less than 3 characters*/ 
    if(keyword.length() < 3) 
    { 
     JOptionPane.showMessageDialog(null, "Keyword has too little characters", 
      "ERROR", JOptionPane.ERROR_MESSAGE); 
     System.exit(0); 
    } 

    keyword = keyword.toUpperCase(); 
    /*Check if not alphabetic*/ 
    for (i= 0; i< keyword.length(); i++) 
    { 
     if(Character.isLetter(keyword.charAt(i)) == false) 
     { 
      JOptionPane.showMessageDialog(null, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
       +" are allowed", 
       "ERROR", JOptionPane.ERROR_MESSAGE); 
      System.exit(0); 
     } 
    } 
    /*Removes all duplicate letters from the keyword*/ 
    String temp = ""; 
    int position = 0; 
    while(position < keyword.length()) 
    { 
     temp = keyword.substring(position + 1); 
     temp = temp.replaceAll(keyword.charAt(position) + "", ""); 
     keyword = keyword.substring(0, position + 1) + temp; 
     position ++; 
    } 
    /*Adds the keyword to the alphabet creating a new alphabet*/ 
    int x=0; 
    String output =(""); 
    String alpha ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    while(x <alpha.length()) 
    { 
     if(keyword.indexOf(alpha.charAt(x)) == -1) 
     { 
     output=output += alpha.charAt(x); 
     } 
     x++; 
    } 

    String map = (keyword + output); 
    System.out.println(map); 

    String newSentence = sentence; 
    newSentence = newSentence.replace(alpha.charAt(0), map.charAt(0)); 
    newSentence = newSentence.replace(alpha.charAt(1), map.charAt(1)); 
    newSentence = newSentence.replace(alpha.charAt(2), map.charAt(2)); 
    newSentence = newSentence.replace(alpha.charAt(3), map.charAt(3)); 
    newSentence = newSentence.replace(alpha.charAt(4), map.charAt(4)); 
    newSentence = newSentence.replace(alpha.charAt(5), map.charAt(5)); 
    newSentence = newSentence.replace(alpha.charAt(6), map.charAt(6)); 
    newSentence = newSentence.replace(alpha.charAt(7), map.charAt(7)); 
    newSentence = newSentence.replace(alpha.charAt(8), map.charAt(8)); 
    newSentence = newSentence.replace(alpha.charAt(9), map.charAt(9)); 
    newSentence = newSentence.replace(alpha.charAt(10), map.charAt(10)); 
    newSentence = newSentence.replace(alpha.charAt(11), map.charAt(11)); 
    newSentence = newSentence.replace(alpha.charAt(12), map.charAt(12)); 
    newSentence = newSentence.replace(alpha.charAt(13), map.charAt(13)); 
    newSentence = newSentence.replace(alpha.charAt(14), map.charAt(14)); 
    newSentence = newSentence.replace(alpha.charAt(15), map.charAt(15)); 
    newSentence = newSentence.replace(alpha.charAt(16), map.charAt(16)); 
    newSentence = newSentence.replace(alpha.charAt(17), map.charAt(17)); 
    newSentence = newSentence.replace(alpha.charAt(18), map.charAt(18)); 
    newSentence = newSentence.replace(alpha.charAt(19), map.charAt(19)); 
    newSentence = newSentence.replace(alpha.charAt(20), map.charAt(20)); 
    newSentence = newSentence.replace(alpha.charAt(21), map.charAt(21)); 
    newSentence = newSentence.replace(alpha.charAt(22), map.charAt(22)); 
    newSentence = newSentence.replace(alpha.charAt(23), map.charAt(23)); 
    newSentence = newSentence.replace(alpha.charAt(24), map.charAt(24)); 
    newSentence = newSentence.replace(alpha.charAt(25), map.charAt(25)); 

    System.out.println(newSentence); 


    /****************************************** 
    * 2nd players Guess 
    * ****************************************/ 

    String guess = JOptionPane.showInputDialog(null,sentence + "\n" + 
     "Player 2 Enter a guess" 
     + "\form example a=b"); 

} 
+0

在這裏張貼了大量的代碼,而無需張貼在錯誤的實際位置的指示之前,首先你也許會希望通過做一些調試或者使用調試器或多個'System.out.println(...)'語句。這些可以幫助您找出問題所在的位置,然後您可以提出一個更具體和可回答的問題(或者可以自己解決問題)。 – 2012-02-18 04:15:56

+0

你應該在最後一節使用數組:newSentance = newSentence.replace(alpha.charAt()...使它更短,更易於閱讀。 – Russell 2012-02-18 04:51:40

+0

for(int i = 0; i <= 25; i ++)newSentence = newSentence.replace(alpha.charAt(i),map.charAt(i));'我不知道你想用你的代碼完成什麼,我只是看着它,儘管「哇,牆上的代碼對你的評價爲1000「,但看起來你需要做大量的工作才能展開一個循環 – jmq 2012-02-18 04:57:44

回答

1

看看字符串全部替換爲正則表達式。你的作業太多重複。簡化它。

+0

我會提出同樣的建議,大多數檢查字數,字符串中的有效字符,結束標點符號,甚至是字符的替換,都可以用正則表達式來完成,而且代碼少得多。 – Tony 2012-02-18 04:21:04

2

apache commons lang library的實用方法replaceChars()「編碼」一個字符串 - 這正是你需要的。

這裏是你如何使用它的一個例子:

String output = StringUtils.replaceChars(input, 
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "TIGERABCDFHJKLMNOPQSUVWXYZ");