Rot13 Java 怎么了?
Rot13 Java what's wrong?
我一直在尝试在 java 中找到自己制作 Rot13 算法的方法,但是当我尝试一个短语时,它给我这个错误:
java.lang.ArrayIndexOutOfBoundsException: 41
所以这是我的代码:
:用全名更新,
public class Rot13
{
char[] translated;
String abc = "abcdefghijklmnopjrstuvwxyzabcdefghijklmnopqrstuvwxyz";
public String ROT13(String input){
input = input.toLowerCase();
char[] sentence = input.toCharArray();
char[] ABC = abc.toCharArray();
int x = input.length();
int y = 0;
char[] translated = new char[x];
for(int i = 0; i<x;i++){
int z = 0;
if(sentence[i] == ' '){
translated[i] = ' ';
}
else {
while(y==0){
if (sentence[i] == ABC[z]){
y =1;
}
else{
z += 1;
}
}
translated[i] = ABC[z+12];
}
}
String rot13string = new String(translated);
return rot13string;
}
}
:更新 2
我刚刚再次测试了这个版本,它翻译了它。但是在错误的方式中,例如 "Hello" 变成 "tmmmm" 第一个字母似乎是正确的,但接下来的总是 'm'.
更新 3:感谢大家的回答,这是我的最终代码,我只是将字母表复制了 "few" 次。 :
public class ROT13
{
char[] translated;
String ab = "abcdefghijklmnopjrstuvwxyzabcdefghijklmnopqrstuvwxyz";
String abc = String.format("%0" + 1000 + "d", 0).replace("0",ab);
public String ROT13(String input){
input = input.toLowerCase();
char[] sentence = input.toCharArray();
char[] ABC = abc.toCharArray();
int length = input.length();
char[] translated = new char[length];
for(int i = 0; i<length;i++){
int y = 0;
int h = 0;
if(sentence[i] == ' '){
translated[i] = ' ';
}
else {
while(y==0){
int z = 0;
if (sentence[i] == ABC[h]){
y +=1;
}
else{
z += 1;
h += 1;
}
}
translated[i] = ABC[h+13];
}
}
String rot13string = new String(translated);
return rot13string;
}
}
目前,想想当句子中有一个'z'时会发生什么。然后,你让 t[i] = ABC[z (26) + 12] //which is larger than ABC's length
.
就个人而言,我会像这样做 rot13:
public char rot13(char s){
if (c >= 'a' && c <= 'm') return c += 13;
else if (c >= 'A' && c <= 'M') return c += 13;
else if (c >= 'n' && c <= 'z') return c -= 13;
else if (c >= 'N' && c <= 'Z') return c -= 13;
else return c;
}
我一直在尝试在 java 中找到自己制作 Rot13 算法的方法,但是当我尝试一个短语时,它给我这个错误:
java.lang.ArrayIndexOutOfBoundsException: 41
所以这是我的代码:
:用全名更新,
public class Rot13
{
char[] translated;
String abc = "abcdefghijklmnopjrstuvwxyzabcdefghijklmnopqrstuvwxyz";
public String ROT13(String input){
input = input.toLowerCase();
char[] sentence = input.toCharArray();
char[] ABC = abc.toCharArray();
int x = input.length();
int y = 0;
char[] translated = new char[x];
for(int i = 0; i<x;i++){
int z = 0;
if(sentence[i] == ' '){
translated[i] = ' ';
}
else {
while(y==0){
if (sentence[i] == ABC[z]){
y =1;
}
else{
z += 1;
}
}
translated[i] = ABC[z+12];
}
}
String rot13string = new String(translated);
return rot13string;
}
}
:更新 2 我刚刚再次测试了这个版本,它翻译了它。但是在错误的方式中,例如 "Hello" 变成 "tmmmm" 第一个字母似乎是正确的,但接下来的总是 'm'.
更新 3:感谢大家的回答,这是我的最终代码,我只是将字母表复制了 "few" 次。 :
public class ROT13
{
char[] translated;
String ab = "abcdefghijklmnopjrstuvwxyzabcdefghijklmnopqrstuvwxyz";
String abc = String.format("%0" + 1000 + "d", 0).replace("0",ab);
public String ROT13(String input){
input = input.toLowerCase();
char[] sentence = input.toCharArray();
char[] ABC = abc.toCharArray();
int length = input.length();
char[] translated = new char[length];
for(int i = 0; i<length;i++){
int y = 0;
int h = 0;
if(sentence[i] == ' '){
translated[i] = ' ';
}
else {
while(y==0){
int z = 0;
if (sentence[i] == ABC[h]){
y +=1;
}
else{
z += 1;
h += 1;
}
}
translated[i] = ABC[h+13];
}
}
String rot13string = new String(translated);
return rot13string;
}
}
目前,想想当句子中有一个'z'时会发生什么。然后,你让 t[i] = ABC[z (26) + 12] //which is larger than ABC's length
.
就个人而言,我会像这样做 rot13:
public char rot13(char s){
if (c >= 'a' && c <= 'm') return c += 13;
else if (c >= 'A' && c <= 'M') return c += 13;
else if (c >= 'n' && c <= 'z') return c -= 13;
else if (c >= 'N' && c <= 'Z') return c -= 13;
else return c;
}