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 {
How to implement Element interchange
Enter 10 integers from the keyboard, put them in a one-dimensional array, and then swap the first 5 elements with the last 5 elements.
That is: The 1th element is interchanged with the 10th element, and the 2nd element is interchanged with the 9th element ... The 5th element is interchanged with the 6th element.
Outputs the values of the original elements of the array, respectively, and the value of each element after the swap.
public static void Main (string[] args) {
Print ();
}
public static int[] Write () {
Creating a cache Array
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 ("Please enter" + (i+1) + "integer");
try {
Array[i]=integer.parseint (Buf[i].readline ());
} catch (NumberFormatException e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("Data input error please re-enter");
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 the array you entered is:");
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 the array after the swap is:");
for (int i = 0; i < ary.length; i++) {
System.out.print (ary[i]+ "");
}
System.out.println ();
}
}
Array change seats