繁体   English   中英

如何循环向下移动字母

[英]How do I shift letters down in a loop

我正在尝试创建一个仅返回字母的循环。 在我的代码中,我得到了不需要的符号。 我该如何修复循环,以便当我的整数为+3时,它只给我字母?

public static String caesarDecrypt(String encoded, int shift){
    String decrypted = "";

    for (int i = 0; i < encoded.length(); i++) {
        char t = encoded.charAt(i);

        if ((t <= 'a') && (t >= 'z')) {
            t -= shift;
        }

        if (t > 'z') {
            t += 26;
        } else if ((t >= 'A') && (t <= 'Z')) {
            t -= shift;

            if (t > 'Z') 
                t += 26;
        } else {

        }

        decrypted = decrypted + t;
    }
}

您正在从字母中减去平移值。 因此,新字母永远不能是> 'z' 您应该检查它是否< 'a' (或分别为'A' )。

StringBuilder decrypted = new StringBuilder(encoded.length());
for (int i = 0; i < encoded.length(); i++)
{
    char t = encoded.charAt(i);
    if ((t >= 'a') && (t <= 'z'))
    {
        t -= shift;
        while (t < 'a')
        {
            t += 26;
        }
    }
    else if ((t >= 'A') && (t <= 'Z'))
    {
        t -= shift;
        while (t < 'A')
        {
            t += 26;
        }
    }

    decrypted.append(t);
}
return decrypted.toString();

另外,您不应该使用String串联来生成结果。 相反,了解StringBuilder

编辑 :要确保新字母在任意(正)移位的'a' .. 'z'范围内,应使用while代替if

我没有给您确切的代码。 但我可以在逻辑上帮助您:

  1. 检查是否由于移动而到达终点( a,A,z,Z )。
  2. 如果您以任何一种方式超过了端点,则计算端点之间的距离并偏移t (根据终点)将此距离与另一个端点相加/减去/取模以获取准确的字母。

像这样吗 (警告,未经测试)

public static String caesarDecrypt(String encoded, int shift) {
    String decrypted = "";
    for (int i = 0; i < encoded.length(); i++) {
        char t = encoded.charAt(i).ToUpper();

        decrypted = decrypted + decode(t, shift);
    }
}


// call with uppercase ASCII letters, and a positive shift
function decode(char n, int shift)
{
   if ((n < 'A') || (n > 'Z')) return ('-');

   var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var s = str.charAt(((n - 'A') + shift)%26);
   return(s);
}

当你在命名你的方法caesarDecrypt (我假设你的意思是加密 ),我想你想的字母表,包括缠绕的转变。

此代码将为您做到这一点:

public class Snippet {
    public static void main(String[] args) {
        System.out.println(caesarShift("This is a Fizzy test.", 5));
        System.out.println(caesarShift("Ymnx nx f Kneed yjxy.", -5));
    }

    public static String caesarShift(String input, int shift) {
        // making sure that shift is positive so that modulo works correctly
        while (shift < 0)
            shift += 26;

        int l = input.length();
        StringBuffer output = new StringBuffer();

        for (int i = 0; i < l; i++) {
            char c = input.charAt(i);
            char newLetter = c;

            if (c >= 'a' && c <= 'z') { // lowercase
                newLetter = (char) ((c - 'a' + shift) % 26 + 'a'); // shift, wrap it and convert it back to char
            } else if (c >= 'A' && c <= 'Z') { // uppercase
                newLetter = (char) ((c - 'A' + shift) % 26 + 'A'); // shift, wrap it and convert it back to char
            }

            output.append(newLetter);
        }

        return output.toString();
    }
}

这将处理小写和大写字母。 其他所有内容将保持不变(例如空格,标点符号等)。

请花一些时间查看此代码,以了解其工作原理。 我已经发表了一些评论以使其更清楚。 从您的代码中,我认为您有点困惑,因此,对此代码的理解非常重要。 如果您有任何疑问,请随时提问。


这段代码

String start = "abcdefghijklmnopqrstuvwxyz";
String encrypted = caesarShift(start, 3);
String decrypted = caesarShift(encrypted, -3);
System.out.println("Start     : " + start);
System.out.println("Encrypted : " + encrypted);
System.out.println("Decrypted : " + decrypted);

会给出这个结果

Start     : abcdefghijklmnopqrstuvwxyz
Encrypted : defghijklmnopqrstuvwxyzabc
Decrypted : abcdefghijklmnopqrstuvwxyz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM