-2

I am having trouble with getting this to work I need pointed in the right direction. The user will input a command line integer to determine how many places the characters will be rotated. My problem is that I want all lower case and upper case to stay the same case along with keeping spaces and special characters untouched, only rotating the letters by the specified integer. I need to make sure that for example: the integer set is 1 and the character "Z" is entered in the string to be encrypted it needs to wrap back around to "A" instead of going to the next ASCII character "[" I have been trying to use the "%" operator but I might be using it wrong. I am not looking for someone to write the entire code for me and I know I could find my answer by looking at the source code of a working program but I am trying to figure as much of this out by myself but right now I am stumped and need a small bit of help. Thanks!

I finally got it to work I had the entire operation wrong this is what worked for me.

char encrypt = ('A' + ( ( (input[i] - 'A') + key ) % 26));

Before I was trying

char encrypt (input[i] + key) % 26;

Thanks for the fast answers!

4

1 回答 1

0

虽然有很多方法可以实现您想要的,但我建议您研究java.util.Map. 确定这是加密Map还是解密,并将输入Map字符“放置”为密钥,将输出字符“放置”为值。然后,当您需要翻译时,您只需遍历每个要翻译的字符并组装输出值。注意未映射的值(因为它们将在查找时返回 null)。

于 2012-10-17T04:45:56.763 回答