以下代码旨在根据ASCII
值解密“ secret ”消息: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
最佳答案
我修好了。问题是 ASCII 值 > 126 不正确,因此简单减去 95 即可修复解密!
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
关于Java - 为什么打印出 "007F"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34216662/