Java 在Caesar密码中引用数组中的字符

Java 在Caesar密码中引用数组中的字符,java,arrays,string,reference,Java,Arrays,String,Reference,我正在努力学习Java,希望有人能给我指出正确的方向。我试图用java构建一个caesar密码程序,我试图使用一个自定义字符串,但在引用由自定义字符串组成的数组时遇到了问题(例如,如果移位为1,“a”将变为“b”,“z”将变为“a”,“?”将变为“@”,等等;实际的自定义字符串列在程序的数组中)。按照我现在使用的程序,它可以移动a-z和a-z,但我需要它继续移动到特殊字符。我知道我现在没有引用我的字符串,但不确定如何让它这样做!非常感谢任何帮助 package caesarcipher; im

我正在努力学习Java,希望有人能给我指出正确的方向。我试图用java构建一个caesar密码程序,我试图使用一个自定义字符串,但在引用由自定义字符串组成的数组时遇到了问题(例如,如果移位为1,“a”将变为“b”,“z”将变为“a”,“?”将变为“@”,等等;实际的自定义字符串列在程序的数组中)。按照我现在使用的程序,它可以移动a-z和a-z,但我需要它继续移动到特殊字符。我知道我现在没有引用我的字符串,但不确定如何让它这样做!非常感谢任何帮助

package caesarcipher;

import java.util.Scanner;

public class Caesarcipher
{
public static void main(String[] args)
{
    Scanner scan = new Scanner (System.in);

    System.out.println("Please enter the shift you would like to use:");

    String shift = scan.next();

    int shiftvalue = Integer.parseInt(shift);

    System.out.println("The shift will be: " + shiftvalue);

    System.out.println("Please enter text:");

    String text = scan.next();

    String ciphertext = encryptCaesar(text, shiftvalue);

    System.out.println(ciphertext);
}
private static String encryptCaesar(String str, int shiftvalue)
{
        if (shiftvalue < 0) 
        {
            shiftvalue = 81 - (-shiftvalue % 81);
        }
    String result = "";
        for (int i = 0; i < str.length(); i++) 
        {
        char ch = encryptCharacter(str.charAt(i), shiftvalue);
        result += ch;
        }
    return result;
}
private static char encryptCharacter(char ch, int shiftvalue) 
{
    String[] values = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " ", "!", "\\", "\"", "#", "$", "%", "&", "'", "(", ")", ",", "-", ".", "/", ":", ";", "?", "@"};  

    if (Character.isLowerCase(ch))
        {
            ch = (char) ('a' + (Character.toLowerCase(ch) - 'a' + shiftvalue) % 81);
        }
    else if (Character.isUpperCase(ch))
        {
            ch = (char) ('a' + (Character.toUpperCase(ch) - 'a' + shiftvalue) % 81);
        }
    return ch;
}
}
package;
导入java.util.Scanner;
公共类密码
{
公共静态void main(字符串[]args)
{
扫描仪扫描=新扫描仪(System.in);
System.out.println(“请输入您要使用的班次:”;
String shift=scan.next();
int-shiftvalue=Integer.parseInt(移位);
System.out.println(“移位将为:“+shiftvalue”);
System.out.println(“请输入文本:”);
String text=scan.next();
字符串密文=EncryptCasar(文本,移位值);
System.out.println(密文);
}
私有静态字符串加密CAESAR(字符串str,int-shiftvalue)
{
如果(移位值<0)
{
移位值=81-(-shiftvalue%81);
}
字符串结果=”;
对于(int i=0;i
您可以对字母输入进行移位。移位值大于26时也适用

public static String encrypt(String str, int shift)
{
String ciphered="";
char[] ch=str.toCharArray();
int exact=shift%26;
    if(exact<0)
    exact+=26;
int rotate=shift/26;
int i=0;
while(i<ch.length)
{
    if(Character.isLetter(ch[i]))
    {
        int check=Character.isUpperCase(ch[i])?90:122;
        ch[i]+=exact;
        if(check<ch[i])
        ch[i]=(char)(ch[i]-26);
        if(rotate%2==0)
        {
            if(Character.isLowerCase(ch[i]))
            ch[i]=Character.toUpperCase(ch[i]);
            else
            ch[i]=Character.toLowerCase(ch[i]);     
        }
    }   
        ciphered+=ch[i++];          
    }
return ciphered;
}
公共静态字符串加密(字符串str,int-shift)
{
字符串加密=”;
char[]ch=str.toCharArray();
整数精确=移位%26;

如果(精确您可以对字母输入进行移位。移位值大于26时也适用

public static String encrypt(String str, int shift)
{
String ciphered="";
char[] ch=str.toCharArray();
int exact=shift%26;
    if(exact<0)
    exact+=26;
int rotate=shift/26;
int i=0;
while(i<ch.length)
{
    if(Character.isLetter(ch[i]))
    {
        int check=Character.isUpperCase(ch[i])?90:122;
        ch[i]+=exact;
        if(check<ch[i])
        ch[i]=(char)(ch[i]-26);
        if(rotate%2==0)
        {
            if(Character.isLowerCase(ch[i]))
            ch[i]=Character.toUpperCase(ch[i]);
            else
            ch[i]=Character.toLowerCase(ch[i]);     
        }
    }   
        ciphered+=ch[i++];          
    }
return ciphered;
}
公共静态字符串加密(字符串str,int-shift)
{
字符串加密=”;
char[]ch=str.toCharArray();
整数精确=移位%26;

谢谢大家!有一个可以依靠的社区太棒了!谢谢大家!有一个可以依靠的社区太棒了!