package com.test;
import java.io.BufferedReader;
import java.io.IOException;import java.io.InputStreamReader;/**
*2017-3-5 下午4:35:40 * @param * @since * @return */public class HuHuanDemo { // 怎样实现元素互换 // 从键盘上输入10个整数,并将其放入一个一维数组中,然后将其前5个元素与后5个元素对换,// 即:第1个元素与第10个元素互换,第2个元素与第9个元素互换…第5个元素与第6个元素互换。// 分别输出数组原来各元素的值和对换后各元素的值。 public static void main(String[] args) { print(); } public static int[] write(){ // 创建缓存数组 BufferedReader[] buf=new BufferedReader[5]; int n; int array[]=new int[5]; for (int i = 0; i < array.length; i++) { buf[i]=new BufferedReader(new InputStreamReader(System.in)); n=1; System.out.print("请输入第"+(i+1)+"个整数"); try { array[i]=Integer.parseInt(buf[i].readLine()); } catch (NumberFormatException e) { // TODO Auto-generated catch block System.out.println("数据输入错误请重新输入"); e.printStackTrace(); break; } catch (IOException e) { // TODO Auto-generated catch block n=0; e.printStackTrace(); } while(n==0); } return array; } public static void print(){ int[] ary=write(); int s; System.out.println("\n你输入的数组是:"); for (int i = 0; i < ary.length; i++) { System.out.print(ary[i]+" "); } for (int i = 0; i < ary.length/2; i++) { s=ary[i]; ary[i]=ary[ary.length-1-i]; ary[ary.length-1-i]=s; } System.out.println("\n对换后的数组是:"); for (int i = 0; i < ary.length; i++) { System.out.print(ary[i]+" "); } System.out.println(); }}