这是我的代码:
package ch3;
import java.util.Scanner;
public class caesarCypher {
static String inp;
public static void main(String[] args) {
int input;
int x = 0;
Scanner scan = new Scanner(System.in);
inp = scan.nextLine();
input = inp.length();
System.out.println(input + inp);
while (x < input) {
x += 1;
inp = ((inp.charAt(x)) - 12);
}
}
}
我该如何将inp
的索引设置为x?因此,换句话来说,我正在尝试减去该字符,例如 ASCII 表中的 a - 12。
最佳答案
尝试使用字符数组:
public class caesarCypher {
static String inp;
public static void main(String[] args) {
int input;
Scanner scan = new Scanner(System.in);
inp = scan.nextLine();
char[] inpArr = inp.toCharArray();
input = inp.length();
System.out.println(input + inp);
for( int i = 0; i < input; i++)
{
inpArr[i] -= 12;
}
inp = new String( inpArr);
}
}
顺便说一下,您将从下面的代码中获得一个 IndexOutOfBoundsException
n 。使用 x,然后递增它。
while (x < input) {
x += 1;
inp = ((inp.charAt(x)) - 12);
}
关于java - 我在尝试获取索引处的字符时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39862825/