java中的getrot_java - Java中的ROT-N(或ROT-X)功能 - 堆栈内存溢出

当前,我正在编写一个使用Java在给定String上执行ROT-1直到包括ROT-25的程序。 在研究的开始,我发现了以下代码:

public class Rot13 {

public static void main(String[] args) {

String s = args[0];

for (int i = 0; i < s.length(); i++) {

char c = s.charAt(i);

if (c >= 'a' && c <= 'm') c += 13;

else if (c >= 'A' && c <= 'M') c += 13;

else if (c >= 'n' && c <= 'z') c -= 13;

else if (c >= 'N' && c <= 'Z') c -= 13;

StdOut.print(c);

}

StdOut.println();

}

}

经过一些麻烦的射击后,我明白了这一点:

private static void rotALL(String input) {

//Loop 25 times, starting with ROT-1 and ending at ROT-25 (every possibliity besides the original input)

for (int i = 1; i < 26; i++) {

int rot = 26 - i;

System.out.print("ROT" + rot + ": ");

for (int charIndex = 0; charIndex < input.length(); charIndex++) {

char c = input.charAt(charIndex);

int inta = 97; //a in the ASCII table

int intaWithRot = inta + rot;

int intA = 65; //A in the ASCII table

int intAWithRot = intA + rot;

int intaWithRotPlusOne = intaWithRot + 1;

int intaWithRotPlusi = intaWithRot + i;

int intAWithRotPlusOne = intAWithRot + 1;

int intAWithRotPlusi = intAWithRot + i;

if (c >= inta && c <= intaWithRot) {

c += rot;

} else if (c >= intA && c <= intAWithRot) {

c += rot;

} else if (c >= intaWithRotPlusOne && c <= intaWithRotPlusi) {

c -= rot;

} else if (c >= intAWithRotPlusOne && c <= intAWithRotPlusi) {

c -= rot;

}

System.out.print(c);

}

System.out.println();

}

现在我要解决问题:

当我使用ROT-13输入“ grfg qngn”(即“测试数据”)时,我对ROT-13的输出为“ ROT13:test d {t {”,“ {”和“ a”相距26位彼此在ASCII表中,但是当字母(例如“ e”)正确显示时,我不知道为什么会发生此错误。

如何更改此算法,使其在ROT-1和ROT-25之间循环? 我认为这应该可以解决问题,但我缺少一些东西。

在此先感谢您的光临!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值