Java循环数组

Java循环数组,java,arrays,shift,Java,Arrays,Shift,如何正确地创建此循环?它现在可以循环,但不能正常循环。它就是这样做的 以下是数字: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 [1] 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 你想换多少个位置?:2 2 1 15 14 13 12 11 10 9 8 7 6 5 4 3 [3] 你想换多少个位置?:4 System.out.println("Here are the numbers:"); for (i=0; i<numbe

如何正确地创建此循环?它现在可以循环,但不能正常循环。它就是这样做的

以下是数字:

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 [1]
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
你想换多少个位置?:2

2 1 15 14 13 12 11 10 9 8 7 6 5 4 3 [3]
你想换多少个位置?:4

System.out.println("Here are the numbers:");
for (i=0; i<numberArray.length; i++) {
    System.out.print(numberArray[i] + " ");
}

while (x != input.nextInt()){

    System.out.printf("How many positions do you want to shift?: ");
    int shiftTimes=input.nextInt();
    for( i = 0; i < shiftTimes; ++i) 

        shift.Shifter(numberArray);

        for(j = 0; j < numberArray.length; j++)
            System.out.printf(numberArray[j]+" ");
        }
    }
}    
[]应该是询问我的输入,而不是我只是输入输入
它应该是这样运行的:

这些数字如下:

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 [1]
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
您想换多少个位置?:1

2 1 15 14 13 12 11 10 9 8 7 6 5 4 3
你想换多少个位置?:4

System.out.println("Here are the numbers:");
for (i=0; i<numberArray.length; i++) {
    System.out.print(numberArray[i] + " ");
}

while (x != input.nextInt()){

    System.out.printf("How many positions do you want to shift?: ");
    int shiftTimes=input.nextInt();
    for( i = 0; i < shiftTimes; ++i) 

        shift.Shifter(numberArray);

        for(j = 0; j < numberArray.length; j++)
            System.out.printf(numberArray[j]+" ");
        }
    }
}    
System.out.println(“以下是数字:”);
对于(i=0;i 0;i--){
列表[i]=列表[i-1];
}
列表[0]=最后一个;
}

这应该适用于右班。它应该与大于数组长度的输入一起工作

for (int i = shiftTimes%numberArray.length; i > 0; i--) {
    System.out.print(numberArray[numberArray.length - i] + " ");
}
for (int i = 0; i < numberArray.length - shiftTimes%numberArray.length; i++) {
    System.out.print(numberArray[i] + " ");
}
更新:将逻辑放入函数中。还更新了无效输入检查

public static void Shifter(int[] list, int input)
{
    for (int i = input%list.length; i > 0; i--) {
        System.out.print(list[list.length - i] + " ");
    }
    for (int i = 0; i < list.length - input%list.length; i++) {
        System.out.print(list[i] + " ");
    }
}

这看起来像是你试图构建的BF编译器。你写了移位器方法吗?@BinaryJudy是的,我写了。我忘记添加了,谢谢你的帮助:)如果你不介意的话,你能添加一个循环代码吗?因为我一直在尝试,但我似乎无法得到它。假设它一直循环,直到我输入字符串