For循环不能很好地工作

For loop doesn't work well

package learning;
import java.util.* ;

public class Learning {

    public static void main(String[] args) {
        String normal , cipher;
        String shiftstr;
        int shiftint, s;

        System.out.println("Welcome To Ceasar Shift Creator");
        Scanner in = new Scanner(System.in);
        normal = in.nextLine();
        char[] proc = normal.toCharArray();
        int length;
        length = normal.length();
        System.out.println("Ok now tell me how many times you want it to be shifted ");
        shiftstr = in.nextLine();
        shiftint = Integer.parseInt(shiftstr);

        s = 0;
        for(int i =0; i < length ; i++){
            while( s < shiftint){
                proc[i]++;
                s++;
            }
            System.out.print(proc[i]);
        }
    }

我希望整个单词向前移动相同的编号。用户提到的次数。但是只有第一个字母被移动了。我知道我做得不太正确,但仍然帮助我...

i为0时,内部while循环只进入一次。这就是为什么只改变proc[0]。

您不需要内部循环:

    for(int i =0; i < length ; i++){
      proc[i]+=shiftint;
      System.out.print(proc[i]);
    }

s 需要在 for 循环中设置回 0。

    for (int i = 0; i < length; i++) {
        while (s < shiftint) {
            proc[i]++;
            s++;
        }
        System.out.print(proc[i]);
        s=0;
    }