以下代码旨在根据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
答案 0 :(得分:1)
我修好了。问题是ASCII值> 126是不正确的所以95的简单减法修正了decyrption!
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;