为什么它不能给我我想要的输出?

时间:2015-03-01 08:25:11

标签: java

我已经在这个项目上工作了大约两个星期但是我似乎无法弄清楚它为什么不能给我

我想要的输出是 -

原判=这是一个非常大的早晨。

加密句子=这是一个ag',rery dug>?\ ijedug>?... w ssadorninjedug>?.. w。

解密的句子=这是一个非常大的早晨。

但我似乎一直在 -

原始句子=这是一个非常大的早晨。

加密句子=这是一个非常挖掘的>?/ ig上午。

解密的句子=这是一个非常大的早晨。

  • 我尝试了很多东西,比如改变字母,完全改变代码,但一直显示出来"这是一个非常挖掘的>?ig morning"。

这是我的代码 -

import java.io.*; 
import java.util.*; 
import java.io.*; 
import java.util.*; 

public class ED { 

public static void main(String args[]) { 
    Scanner kbReader = new Scanner(System.in); 
    System.out.print("Enter a sentence that is to be encrypted:"); 
    String sntnc = kbReader.nextLine(); 
    System.out.println("Original Sentence = " + sntnc); 

    Crypto myCryptObj = new Crypto(); 
    String encryptdSntnc = myCryptObj.encrypt(sntnc); 
    System.out.println("Encrypted sentence = " + encryptdSntnc); 

    String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc); 
    System.out.println("Decrypted sentence = " + decryptdSntnc); 
 } 
} 

class Crypto { 

public String x1; 

public String acceptor(String sntnc) { 
    String x1 = sntnc; 
    return x1; 
} 

public String encrypt(String sntnc) { 
    x1 = sntnc.replace("v", "ag',r"); 
    x1 = sntnc.replace("V", "ag',r"); 
    x1 = sntnc.replace("M", "ssad"); 
    x1 = sntnc.replace("m", "ssad"); 
    x1 = sntnc.replace("g", "jeb..w"); 
    x1 = sntnc.replace("G", "jeb..w"); 
    x1 = sntnc.replace("B", "dug>?/"); 
    x1 = sntnc.replace("b", "dug>?/"); 
    return x1; 
} 

public String decrypt(String sntnc) { 
    x1 = sntnc.replace("ag',r", "v"); 
    x1 = sntnc.replace("ag',r", "V"); 
    x1 = sntnc.replace("ssad", "M"); 
    x1 = sntnc.replace("ssad", "m"); 
    x1 = sntnc.replace("jeb..w", "g"); 
    x1 = sntnc.replace("jeb..w", "G"); 
    x1 = sntnc.replace("dug>?/", "B"); 
    x1 = sntnc.replace("dug>?/", "b"); 
    return x1; 
} 
}

3 个答案:

答案 0 :(得分:2)

Java的Stringimmutable。你无法改变它们。

因此,您必须在加密/解密后返回一个新字符串。

幸运的是,导致新字符串的所有String方法实际上都会创建一个新字符串。 replace包括在内。

所以使用这个:

public String encrypt(String sntnc) { 
    x1 = sntnc.replace("v", "ag',r"); 
    x1 = x1.replace("V", "ag',r"); 
    x1 = x1.replace("M", "ssad"); 
    x1 = x1.replace("m", "ssad"); 
    x1 = x1.replace("g", "jeb..w"); 
    x1 = x1.replace("G", "jeb..w"); 
    x1 = x1.replace("B", "dug>?/"); 
    x1 = x1.replace("b", "dug>?/"); 
    return x1; 
} 

public String decrypt(String sntnc) { 
    x1 = sntnc.replace("ag',r", "v"); 
    x1 = x1.replace("ag',r", "V"); 
    x1 = x1.replace("ssad", "M"); 
    x1 = x1.replace("ssad", "m"); 
    x1 = x1.replace("jeb..w", "g"); 
    x1 = x1.replace("jeb..w", "G"); 
    x1 = x1.replace("dug>?/", "B"); 
    x1 = x1.replace("dug>?/", "b"); 
    return x1; 
} 

或更短(确保使用IDE格式化):

public String encrypt(String sntnc) { 
    return sntnc.replace("v", "ag',r").replace("V", "ag',r").replace("M", "ssad").replace("m", "ssad").replace("jeb..w", "g").replace("jeb..w", "G").replace("dug>?/", "B").replace("dug>?/", "b");  

public String decrypt(String sntnc) { 
    return sntnc.replace("ag',r", "v").replace("ag',r", "V").replace("ssad", "M").replace("ssad", "m").replace("jeb..w", "g").replace("jeb..w", "G").replace("dug>?/", "B").replace("dug>?/", "b"); 
} 

替换顺序在这里很重要,所以要小心

答案 1 :(得分:1)

您只看到'b'字符的加密的原因是因为在您的加密和解密中,您为x1分配了在sntnc上执行替换的结果,但是您继续对每一行使用sntnc.replace(),因此,如果您单步执行调试器,您将看到每个替换正在发生,但是当您返回x1时,它只有最后一个替换。 要获得您正在寻找的结果,您需要在x1而不是sntnc上执行每个后续替换。或者,您可以将sntnc分配给x1和。在encrypt()和decrypt()中,首先将sntnc分配给x1并用x1.replace()替换所有sntnc.replace()。

答案 2 :(得分:0)

Test Code Here

如果输入不包含数字,这应该可以解决问题:

/* package whatever; // don't place package name! */

import java.io.*; 
import java.util.*; 
import java.io.*; 
import java.util.*; 

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{

    public static void main(String args[])
    { 
        Scanner kbReader = new Scanner(System.in); 
        System.out.print("Enter a sentence that is to be encrypted:"); 
        String sntnc = kbReader.nextLine(); 
        System.out.println("\nOriginal Sentence = " + sntnc); 

        Crypto myCryptObj = new Crypto(); 
        String encryptdSntnc = myCryptObj.encrypt(sntnc); 
        System.out.println("Encrypted sentence = " + encryptdSntnc); 

        String decryptdSntnc = myCryptObj.decrypt(encryptdSntnc); 
        System.out.println("Decrypted sentence = " + decryptdSntnc); 
     }

} 

class Crypto
{
    public String x1; 

    public String acceptor(String sntnc)
    { 
        String x1 = sntnc; 
        return x1; 
    } 

    public String encrypt(String sntnc)
    { 
        x1 = sntnc;

        x1 = x1.replaceAll("[vV]", "1"); // 1
        x1 = x1.replaceAll("[mM]", "2"); // 2
        x1 = x1.replaceAll("[gG]", "3"); // 3
        x1 = x1.replaceAll("[bB]", "4"); // 4

        x1 = x1.replaceAll("1", "ag',r");
        x1 = x1.replaceAll("2", "ssad");
        x1 = x1.replaceAll("3", "jeb..w");
        x1 = x1.replaceAll("4", "dug>?/");



        return x1;
    } 

    public String decrypt(String sntnc)
    {
        x1 = sntnc;

        x1 = x1.replaceAll("dug\\>\\?/","4");
        x1 = x1.replaceAll("jeb\\.\\.w","3");
        x1 = x1.replaceAll("ssad","2");
        x1 = x1.replaceAll("ag',r","1");

        x1 = x1.replaceAll("4","B");
        x1 = x1.replaceAll("3","G");
        x1 = x1.replaceAll("2","M");
        x1 = x1.replaceAll("1","V");

        return x1;
    }
}