改变Java中凯撒移位的方向 [英] Changing the direction of a caesar shift in Java

查看:100
本文介绍了改变Java中凯撒移位的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户可以选择向左或向右移动字母,方法是选择左边的1或右边的2。左边工作正常,右边不是。现在它显示完全相同的循环,但我玩过以不同方式更改所有 + - 标志我总是得到奇怪的角色。如何让程序在相反的方向上移动字符?如果用户键入 Hi ,并且移位值为1且方向正确,则 H 应变为 G i 应该变为 k ,作为转移金额应该在每个字母之前增加 shiftValue 。此外,第一个字母目前是不变的,情况应该不是这样。

The user can choose to shift the letters either left or right by choosing 1 for left or 2 for right. Left is working fine, right is not. Right now it shows exactly the same loop, but I have played with changing all the + and - signs in different ways and I always end up getting strange characters. How can I get the program to shift the characters in the opposite direction? If the user types Hi, and the shift value is one and direction is right, the H should become G and the i should become k, as the amount to shift by should be increased by shiftValue before each letter. Also, the first letter currently is unaltered, which should not be the case.

public static String rotate(String userString, int shiftValue, int shiftDirection) {
    int myShift = 0;
    shiftValue = shiftValue % 26 + 26;
    StringBuilder encoded = new StringBuilder();
    if (shiftDirection == 1) {
        for (char i : userString.toCharArray()) {
            if (Character.isLetter(i)) {
                if (Character.isUpperCase(i)) {
                    encoded.append((char) ('A' + (i - 'A' + myShift) % 26));
                } else {
                    encoded.append((char) ('a' + (i - 'a' + myShift) % 26));
                }
            } else {
                encoded.append(i);
            }
            myShift = (myShift + shiftValue) % 26;
        }
    } else if (shiftDirection == 2) {
        for (char i : userString.toCharArray()) {
            if (Character.isLetter(i)) {
                if (Character.isUpperCase(i)) {
                    encoded.append((char) ('A' + (i - 'A' + myShift) % 26));
                } else {
                    encoded.append((char) ('a' + (i - 'a' + myShift) % 26));
                }
            } else {
                encoded.append(i);
            }
            myShift = (myShift - shiftValue) % 26;
        }
    }

    return encoded.toString();
}


推荐答案

在你的代码中,你设置了 myShift 在开始时为0,在转移第一个字母时没有更改。这就是为什么你的第一个字母没有改变。

In your code, you set myShift to 0 in the beginning and didn't change it when shifting your first letter. That is why your first letter is unaltered.

如果你转发的话,在实际轮换之前这样做:

Do this before the actual rotation if you are shifting forward:

int myShift = shiftValue % 26 + 26;






我之前的问题已经回答了你的问题,
每次轮班读取一封信

您可以为 rotate 方法使用重载方法。

You can use an overloaded method for your rotate method.

public static String rotate(String userString, int shiftValue, int shiftDirection) {
    if (shiftDirection == 1) {
        return rotate(userString, shiftValue);
    }
    else if (shiftDirection == 2) {
        return rotate(userString, -shiftValue);
    }
    else {
        return "This is not a valid way to shift your message.";
    }
}

其他轮换方法将是重复的代码:

The other rotate method would be the code that is repeated:

public static String rotate(String userString, int shiftValue) {
   StringBuilder encoded = new StringBuilder();
   int myShift = shiftValue % 26 + 26;
   for (char i : userString.toCharArray()) {
       if (Character.isLetter(i)) {
            if (Character.isUpperCase(i)) {
                encoded.append((char) ('A' + (i - 'A' + myShift) % 26 ));
            } else {
                encoded.append((char) ('a' + (i - 'a' + myShift) % 26 ));
            }
        } else {
            encoded.append(i);
        }
        myShift = (myShift + shiftValue) % 26;
    }
    return encoded.toString();
}

请注意,这两种方法具有相同的名称,但它们采用不同的参数。调用方法时,请使用此方法。然后,您可以指定要转移邮件的方式,而无需担心更改重复代码中的符号。

Note that the two methods have the same name, but they take different parameters. When you call the method, use this one. Then you can specify which way you want to shift your message without worrying about changing the signs in your repeating code.

还有其他两种方法可以使其发挥作用。

There are two other ways to make this work.

首先,当 shiftDirection == 2 时,你应该替换

First, when shiftDirection == 2, you should replace

encoded.append((char) ('A' + (i - 'A' + myShift) % 26));

with

encoded.append((char) ('A' + (i - 'A' - myShift) % 26));

所以 shiftDirection == 2 应该

for (char i : userString.toCharArray()) {
        if (Character.isLetter(i)) {
            if (Character.isUpperCase(i)) {
                encoded.append((char) ('A' + (i - 'A' - myShift) % 26));
            } else {
                encoded.append((char) ('a' + (i - 'a' - myShift) % 26));
            }
        } else {
            encoded.append(i);
        }
        myShift = (myShift + shiftValue) % 26;
    }






其次,你可以拥有通过在开头将 myShift 设置为 -shiftValue ,方向向后移动,并保持其他所有内容相同。 shiftDirection == 2 时的完整代码应为


Second, you can have the direction shift backwards by setting myShift to -shiftValue in the beginning and keep everything else the same. The complete code for when shiftDirection == 2 should be

myShift = -shiftValue;
for (char i : userString.toCharArray()) {
        if (Character.isLetter(i)) {
            if (Character.isUpperCase(i)) {
                encoded.append((char) ('A' + (i - 'A' + myShift) % 26));
            } else {
                encoded.append((char) ('a' + (i - 'a' + myShift) % 26));
            }
        } else {
            encoded.append(i);
        }
        myShift = (myShift - shiftValue) % 26;
    }

这篇关于改变Java中凯撒移位的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆