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

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

/**
 * 
 */
package com.gem.demo.day04_practice;

import java.util.Scanner;

/**
 *
 * Description:3.有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
 *
 * @author HadwinLing
 *
 * @date 2020年1月13日下午6:54:20
 *
 * @version 0.0.1 
 *
 */
public class practice03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.println("请输入数组长度");
		int len = input.nextInt();
		System.out.println("请输入要转换的长度");
		int m = input.nextInt();
		int [] arry =new int[len];
		arry = getArry(arry);
		function(arry,m);
		printArry(arry);
		

	}
	public static void function(int []arry ,int m) {
		int len = arry.length;
		int temp = arry[len-1];
		 
		for(int i = len-1;i>0;i--) {
			arry[i] = arry[i-1]; 
		}
		arry[0]= temp;
		m--;
		if(m>0) {
			function(arry,m);
		}
		
	}
	public static int[] getArry(int[] arry) {
		Scanner input = new Scanner(System.in);
		int len = arry.length;
		for(int i=0;i<len ;i++) {
			System.out.print("请输入第"+(i+1)+"个数");
			arry[i] = input.nextInt();
		}
		return arry;
	}
	public static void printArry(int [] arry) {
		for (int i : arry) {
			System.out.print(i+"\t");
		}
	}

}

发布了42 篇原创文章 · 获赞 12 · 访问量 6124

猜你喜欢

转载自blog.csdn.net/Alingyuzi/article/details/103967697
今日推荐