[20-04-29][Self-test 21]Java Excursion
作者:互联网
1 package test_3_5; 2 3 import java.util.Scanner; 4 5 public class Excursion { 6 7 public static void main(String[] args) { 8 9 /** 有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数 */ 10 int[] numArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 11 12 System.out.println("请输入要偏移的个数:"); 13 Scanner sc = new Scanner(System.in); 14 int m = sc.nextInt(); 15 16 excurse(numArray, m); 17 18 } 19 20 private static void excurse(int[] numArray, int m) { 21 22 int[] newNumArr = new int[numArray.length]; 23 24 for (int i = 0; i < newNumArr.length - m; i++) { 25 newNumArr[i] = numArray[i + m]; 26 } 27 28 for (int i = 0; i < m; i++) { 29 newNumArr[newNumArr.length - m + i] = numArray[i]; 30 } 31 32 for (int i = 0; i < newNumArr.length; i++) { 33 System.out.print(newNumArr[i] + " "); 34 } 35 } 36 37 }
结果如下:
请输入要偏移的个数:
3
4 5 6 7 8 9 10 1 2 3
标签:10,20,21,04,numArray,int,System,length,newNumArr 来源: https://www.cnblogs.com/mirai3usi9/p/12803162.html