Java 中的 ROT13 不想正常工作
ROT13 in Java doesn´t want to work properly
有时我在 Java 中编程 ROT13 时遇到问题。所以用户应该写他想写的任何东西,程序应该在 ROT13 中重写它。所以这是我到目前为止的程序:
import java.io.*;
public class rot13
{
public static void main (String [] args) throws IOException
{
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed
String key [] = {"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"};
String keyA [] = {"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"};
String letter;
System.out.println("Enter a phrase:");
String phrase = myInput.readLine();
int y = 0, i = 0;
while ( y <= phrase.length()){
letter = Character.toString(phrase.charAt(y));
while(i <= y){
if (letter != key[i]){
keyA [i] = keyA[i];
}
i++;
}
System.out.println(keyA [i]);
y++;
}
}
}
问题如下:
它只需要几个字母,但在 3 行之后或更确切地说是在 3 个后者之后停止工作,并提出以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(Unknown Source)
at rot13.main(rot13.java:19)
我试过不同的词,但它总是打印出同样的问题。有谁知道如何修复它,或者至少知道如何更仔细地修复它?
提前致谢!!
为什么不起作用
import java.io.*;
public class rot13
{
public static void main (String [] args) throws IOException
{
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed
String key [] = {"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"};
String keyA [] = {"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"};
String letter;
System.out.println("Enter a phrase:");
String phrase = myInput.readLine();
int y = 0, i = 0;
while ( y <= phrase.length()){
letter = Character.toString(phrase.charAt(y));
//Each time you go throught the first loop, you are comparing your actual position in the string and i
//But as you don't reset i back to 0, you only try to compare your previous index and your actual index : if y == 3, so i takes only the values 2 and 3
//Moreover, when y > 26, you try to access the key array outside of its bounds
while(i <= y){
// letter is a string so you should be using equals
if (letter != key[i]){
// You are putting the value at the i index in the i index, so you do basically nothing with this line
keyA [i] = keyA[i];
}
i++;
}
System.out.println(keyA [i]);
y++;
}
}
}
备选
您可以使用以下解决方案:
import java.io.*;
public class rot13 {
public static void main(String[] args) throws IOException {
BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));// Buffered Reader reads the number inputed
System.out.println("Enter a phrase:");
String input = myInput.readLine();
//We loop through every char in the string
for (char c : input.toCharArray()) {
//We check if the character is a letter, so we don't add the offset to special characters like space or dot
if (Character.isAlphabetic(c)) {
//Here we get the lower case version of the char and remove it 97, which is the ascii value of 'a'
//With this, we are mapping letters from a to z to numbers from 0 to 25
char lowerChar = (char) (Character.toLowerCase(c) - 97);
//We add the offset of 13
lowerChar += 13;
//We then use the modulo to move numbers higher than 15 back to the beginning
lowerChar %= 26;
//We finally come back to the ascii value of our lower case char
lowerChar += 97;
System.out.print(Character.isUpperCase(c) ? Character.toUpperCase(lowerChar) : lowerChar);
} else {
//If it's not a letter, we just print the char
System.out.print(c);
}
}
//We don't forget to close our BuffererReader
myInput.close();
}
}
这是描述的版本,但您可以通过在一行中执行所有字符操作来缩短它
有时我在 Java 中编程 ROT13 时遇到问题。所以用户应该写他想写的任何东西,程序应该在 ROT13 中重写它。所以这是我到目前为止的程序:
import java.io.*;
public class rot13
{
public static void main (String [] args) throws IOException
{
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed
String key [] = {"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"};
String keyA [] = {"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"};
String letter;
System.out.println("Enter a phrase:");
String phrase = myInput.readLine();
int y = 0, i = 0;
while ( y <= phrase.length()){
letter = Character.toString(phrase.charAt(y));
while(i <= y){
if (letter != key[i]){
keyA [i] = keyA[i];
}
i++;
}
System.out.println(keyA [i]);
y++;
}
}
}
问题如下: 它只需要几个字母,但在 3 行之后或更确切地说是在 3 个后者之后停止工作,并提出以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(Unknown Source)
at rot13.main(rot13.java:19)
我试过不同的词,但它总是打印出同样的问题。有谁知道如何修复它,或者至少知道如何更仔细地修复它?
提前致谢!!
为什么不起作用
import java.io.*;
public class rot13
{
public static void main (String [] args) throws IOException
{
BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));// Buffered Reader reads the number inputed
String key [] = {"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"};
String keyA [] = {"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"};
String letter;
System.out.println("Enter a phrase:");
String phrase = myInput.readLine();
int y = 0, i = 0;
while ( y <= phrase.length()){
letter = Character.toString(phrase.charAt(y));
//Each time you go throught the first loop, you are comparing your actual position in the string and i
//But as you don't reset i back to 0, you only try to compare your previous index and your actual index : if y == 3, so i takes only the values 2 and 3
//Moreover, when y > 26, you try to access the key array outside of its bounds
while(i <= y){
// letter is a string so you should be using equals
if (letter != key[i]){
// You are putting the value at the i index in the i index, so you do basically nothing with this line
keyA [i] = keyA[i];
}
i++;
}
System.out.println(keyA [i]);
y++;
}
}
}
备选
您可以使用以下解决方案:
import java.io.*;
public class rot13 {
public static void main(String[] args) throws IOException {
BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));// Buffered Reader reads the number inputed
System.out.println("Enter a phrase:");
String input = myInput.readLine();
//We loop through every char in the string
for (char c : input.toCharArray()) {
//We check if the character is a letter, so we don't add the offset to special characters like space or dot
if (Character.isAlphabetic(c)) {
//Here we get the lower case version of the char and remove it 97, which is the ascii value of 'a'
//With this, we are mapping letters from a to z to numbers from 0 to 25
char lowerChar = (char) (Character.toLowerCase(c) - 97);
//We add the offset of 13
lowerChar += 13;
//We then use the modulo to move numbers higher than 15 back to the beginning
lowerChar %= 26;
//We finally come back to the ascii value of our lower case char
lowerChar += 97;
System.out.print(Character.isUpperCase(c) ? Character.toUpperCase(lowerChar) : lowerChar);
} else {
//If it's not a letter, we just print the char
System.out.print(c);
}
}
//We don't forget to close our BuffererReader
myInput.close();
}
}
这是描述的版本,但您可以通过在一行中执行所有字符操作来缩短它