Java--有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数

import java.util.Scanner;

public class Test {
	
	private static Scanner input;

	public static void main(String[] args) {
		//随机数组大小
		int n = (int)(Math.random() * 5 + 10);
		int[] array = new int[n];
		//随机装入数
		for (int i = 0; i < array.length; i++) {
			array[i] = (int)(Math.random() * 90 + 10);
		}
		//打印
		System.out.print("总共有"+n +"个数,原顺序为:");
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + ",");
		}
		//后移m位
		input = new Scanner(System.in);
		System.out.println();
		System.out.print("你想要把该数组后移几位呢:");
		int m = input.nextInt();
		System.out.print("移动"+ m + "位的顺序为:");
		
		for (int i = 0; i < array.length; i++) {
			//后移几位就打印后面被移除去的到前面
			if (i < m) {
				System.out.print(array[array.length-m+i]+",");
			//再打印原素组第一位被移到后面的数
			}else if(i >= m){
				System.out.print(array[i-m] +",");
			}
		}
	}

}

你可能感兴趣的:(java题)