Java - 为什么" 007F"打印出来?

时间:2015-12-11 05:23:49

标签: java system.out

以下代码旨在根据ASCII值解密"秘密" message :mmZ\\dxZmx]Zpgy

应该打印出什么:"黎明时攻击!"

目前打印出来的内容:"在007F黎明时攻击007F?"

基本上,现在x = "007F"y = "?"

我需要x = SPACE或" "和y = "!"

感谢您的时间。

public class decryption
{
    public static void main(String[] args)
    {
        String secretMessage = ":mmZ\\dxZmx]Zpgy";
        System.out.println(decryption(secretMessage, 88));
    }//end main

    public static String decryption(String s, int n)
    {
        int originalChar, decryptedChar;
        String message = "";
        char c;

        for(int i = 0; i < s.length(); ++i)
        {
            c = s.charAt(i);
            decryptedChar = (int)c;
            if(decryptedChar + n > 126)
               originalChar = 32 + ((decryptedChar + n) - 113);
                  else
                     {originalChar = decryptedChar + n;
                     c = c;}
            message = message + (char)originalChar;
        }//end for loop
        return message;
    }//end method
 }//end class

1 个答案:

答案 0 :(得分:1)

我修好了。问题是ASCII值> 126是不正确的所以95的简单减法修正了decyrption!

&#13;
&#13;
public class decryption
{
public static void main(String[] args)
{
	String secretMessage = ":mmZ\\dxZmx]Zpgy";
	System.out.println(decryption(secretMessage, 88));
}//end main

public static String decryption(String s, int n)
{
	int originalChar, decryptedChar;
	String message = "";
	char c;

	for(int i = 0; i < s.length(); ++i)
	{
		c = s.charAt(i);
		decryptedChar = (int)c;
		if(decryptedChar + n > 126)
		   originalChar = 32 + ((decryptedChar + n) - 113);
			    else
				 {originalChar = decryptedChar + n;
				 c = c;}
      if (originalChar > 126)
      originalChar = originalChar - 95;
		message = message + (char)originalChar;
	}//end for loop
	return message;
}//end method
}//end class
&#13;
&#13;
&#13;