以下代码旨在根据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
最佳答案
我修好了它。问题在于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 - Java-为什么要打印“007F”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34216662/