如何在Java中将数组字符移到右侧?

这就是我所拥有的:

class encoded { public static void main(String[] args) { String s1 = "hello"; char[] ch = s1.toCharArray(); for(int i=0;i<ch.length;i++) { char c = (char) (((i - 'a' + 1) % 26) + 'a'); System.out.print(c); } } } 

到目前为止,我已经将字符串转换为数组,并且我已经研究了如何移动,但现在我被卡住了。

我想要的是代码从ch[0] ,读取字符,将其向右移动( hi ),然后对数组中的每个字符执行相同操作,直到它到达结尾。

现在,我的代码输出opqrs 。 我希望它输出ifmmp 。 如果我用for int i = ch[0]替换for循环中的int i = ch[0] ,它确实从i开始,但是它只输入ijklmno...

我希望它读取h ,输出为i ,读取e ,输出为f ,依此类推,直到它到达数组的末尾。

您正在使用循环索引i而不是循环中的第i个字符,这意味着代码的输出不依赖于输入String (除了输出的长度,它与长度相同)输入)。

更改

 char c = (char) (((i - 'a' + 1) % 26) + 'a'); 

 char c = (char) (((ch[i] - 'a' + 1) % 26) + 'a'); 

ch[i] - 'a' + 1替换i - 'a' + 1 ch[i] - 'a' + 1

 class encoded { public static void main(String[] args) { String s1 = "hello"; char[] ch = s1.toCharArray(); for(int i=0;i