已知一个数组,求数组中心元素

时间:2017-04-14 00:24:13   收藏:0   阅读:1589

/**
*
*/
package Student_System;
import java.util.*;
import java.util.*;
/**Homework11
*
*Homework1101
*已知一个数组,求数组中心元素
* @author 读你一世
*
* QQ: 1816274408
*2017年4月11日上午10:25:03
*
*/
public class Homework1101 {


public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.print("请输入你要输入数组的元素个数:");
int number =scan.nextInt();
int[] a=new int [number];

System.out.print("请输入你的数组:\n");
for(int i=0;i<number;i++){

a[i] =scan.nextInt();
}
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");

}

if(a.length%2==0){
System.out.print("该数组的中心元素为:" +a[a.length/2]+ " 和 " +a[a.length/2+1]);
}else{
System.out.print("该数组的中心元素为:" +a[a.length/2]);
}
}

}

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!