2015-02-10 201 views
0

我可以知道我应该怎么做,如果我想加密40个字符,但我只能加密与我做的代码的第一个字符。加密字符串

System.out.println("Enter character: "); 
    sentence = scan.nextLine(); 
    String random; 
    System.out.println("Enter random character: "); 
    random = scan.nextLine(); 


    conv = random.charAt(sentence.indexOf(sentence)); 
    back = sentence.charAt(random.indexOf(conv)); 

    System.out.println("U want to encrypt or decrypt?"); 
    answer = scan.nextLine(); 



    if(answer.equals("encrypt")) 
    { 
    System.out.println("The original character is:" +sentence); 
    System.out.println("The encrypted character is:" +conv); 
    } 
    else 
    { 
    System.out.println("The decrypted character is:" +back); 

    } 
    // TODO code application logic here 
    } 

}

回答

0

很明显,当你excuting语句只有一次只有一个字符将得到加密。您需要使用循环遍历输入字符串中的所有字符。我给大家举一个例子:

String conv = ""; 
for (int i = 0; i < senentence.length(); i++) { 
    conv.append(...); 
} 

你追加什么,取决于你所使用的算法和输入的字符串有多长。使用random.charAt(i)只会使用随机字符串,并最终抛出异常。