Array change seats

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.