Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
3年前关闭。
我是Java新手,必须编写Caesar Cipher程序。这是我的代码。我遇到的问题是我不能将它移位超过1。例如,如果我写“ help me”并输入shift值2,它仍然只移位1->'ifmq nf'
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
3年前关闭。
我是Java新手,必须编写Caesar Cipher程序。这是我的代码。我遇到的问题是我不能将它移位超过1。例如,如果我写“ help me”并输入shift值2,它仍然只移位1->'ifmq nf'
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str;
String key;
int keyLength;
System.out.println("Enter message:");
str=sc.nextLine();
System.out.println("Enter encryption key:");
key=sc.next();
keyLength=key.length();
//This for loop is repeated use of 'Enrypt' and 'Decrypt' options
for(;;)
{
System.out.println("1.Encrypt\n2.Decrypt\n3.Exit...");
int choice=sc.nextInt();
switch(choice)
{
case 1:
/*send input string keyLength to encrypt() method to encrypt it returns 'Encrypted' string*/
System.out.println("Encrypted message..."+encrypt(str,keyLength));
break;
case 2:
//send retrived string from encrypt() method and keyLength to decrypt() method it returns 'Decrypted' string
System.out.println("Decryptedmessage..."+decrypt(encrypt(str,keyLength),keyLength));
break;
case 3:
//exit from the program
System.exit(0);
break;
default:
System.out.println("Invalid option..");
}
}
}
public static String encrypt(String str,int keyLength)
{
String encrypted="";
for(int i=0;i<str.length();i++)
{
//stores ascii value of character in the string at index 'i'
int c=str.charAt(i);
//encryption logic for uppercase letters
if(Character.isUpperCase(c))
{
c=c+(keyLength%26);
//if c value exceeds the ascii value of 'Z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c>'Z')
c=c-26;
}
//encryption logic for lowercase letters
else if(Character.isLowerCase(c))
{
c=c+(keyLength%26);
//if c value exceeds the ascii value of 'z' reduce it by subtracting 26(no.of alphabets) to keep in boundaries of ascii values of 'a' and 'z'
if(c>'z')
c=c-26;
}
//concatinate the encrypted characters/strings
encrypted=encrypted+(char) c;
}
return encrypted;
}
public static String decrypt(String str,int keyLength)
{
String decrypted="";
for(int i=0;i<str.length();i++)
{
//stores ascii value of character in the string at index 'i'
int c=str.charAt(i);
//decryption logic for uppercase letters
if(Character.isUpperCase(c))
{
c=c-(keyLength%26);
//if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c<'A')
c=c+26;
}
//decryption logic for uppercase letters
else if(Character.isLowerCase(c))
{
c=c-(keyLength%26);
//if c value deceed the ascii value of 'A' increase it by adding 26(no.of alphabets) to keep in boundaries of ascii values of 'A' and 'Z'
if(c<'a')
c=c+26;
}
//concatinate the decrypted characters/strings
decrypted=decrypted+(char) c;
}
return decrypted;
}
最佳答案
正如Scary Wombat和MadProgrammer所说,在第12行上您有keyLength=key.length();
。这将返回您输入的String
的长度。因此,如果输入2,则keyLength
为1。如果输入11,则keyLength
为2。
我将完全摆脱keyLength
,将key
设置为int
而不是String
,然后设置key=sc.nextInt();
这将允许1等于1,2等于2,依此类推。
记住将所有keyLength
实例更改为key
关于java - 不能将Caesar Cipher移位1以上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41580795/
10-10 04:17