Java Array 5 jobs (2015-8-27)

Source: Internet
Author: User

1, from the keyboard input 7 numbers into a one-dimensional array, the first output subscript an odd number of elements, and then output subscript an even-numbered element.

Import Java.util.Scanner;

public class secondtest{

public static void Main (string[] args) {

--Defining a one-dimensional array

int[] arr = new INT[7];

Scanner scan = new Scanner (system.in);

for (int i=0;i<7;i++) {

System.out.print ("Please enter" + (i+1) + "data:");

int number = Scan.nextint ();

Arr[i] = number;

}

--Output: subscript is an odd number of data

System.out.print ("Odd subscript data is:");

for (int i=0;i<arr.length;i++) {

if (i%2!=0) {

System.out.print (arr[i]+ "");

}

}

System.out.println ();

--Output: subscript an even number of data

System.out.print ("Even subscript data is:");

for (int i=0;i<arr.length;i++) {

if (i%2==0) {

System.out.print (arr[i]+ "");

}

}  } }

2, from the keyboard input 7 numbers into a one-dimensional array, the subscript is the odd value of the element to be re-credited to the array, and output all elements.

Import Java.util.Scanner;

public class threetest{

public static void Main (string[] args) {

--Defining a one-dimensional array

double[] arr = new DOUBLE[7];

Scanner scan = new Scanner (system.in);

for (int i=0;i<7;i++) {

System.out.print ("Please enter" + (i+1) + "data:");

Double number = scan.nextdouble ();

Arr[i] = number;

}

--Odd subscript Countdown

for (int i=0;i<arr.length;i++) {

if (i%2!=0) {

--Add 0 to the odd position!

if (arr[i]!=0) {

Double number = 1/arr[i];

Arr[i] = number;

}

}

}

for (int i=0;i<arr.length;i++) {

System.out.print (arr[i]+ "");

}

} }

3, from the keyboard input 6 numbers into one-dimensional array A, and then the values in the reverse order in the array B and output

Import Java.util.Scanner;

public class ftest{public static void Main (string[] args) {

a--1,2,3//b--3,2,1

Int[] A = new int[6];

Int[] B = new Int[6];

int j = 0;

Scanner scan = new Scanner (system.in);

for (int i=0;i<6;i++) {

System.out.print ("Please enter" + (i+1) + "data:");

int number = Scan.nextint ();

A[i] = number;

}

for (int i= (a.length-1); i>=0;i--) {

B[J] = A[i];

j + +;

}

System.out.print (The data in "a" is: ");

for (int i=0;i<a.length;i++) {

System.out.print (a[i]+ "");

}

System.out.println ();

System.out.print (The data in "B" is: ");

for (int i=0;i<b.length;i++) {

System.out.print (b[i]+ ""); }

} }

4. Select Sort instruction and enter 5 numbers from the keyboard, sort in descending order by selection method and output

Import Java.util.Scanner;

public class fivetest{

public static void Main (string[] args) {

--Define array int[] arr = new INT[5];

--Enter 5 data from the keyboard:

Scanner scan = new Scanner (system.in);

for (int i=0;i<5;i++) {

System.out.print ("Please enter" + (i+1) + "data:");

int number = Scan.nextint ();

Arr[i] = number;

}

SYSTEM.OUT.PRINTLN ("All data input complete!!! ");

-----Select Sort---------

--Define subscript for minimum data

int min_index = 0;

for (int i=0;i<arr.length-1;i++) {

Min_index = i;

for (int j=i+1;j<arr.length;j++) {

if (Arr[min_index]>arr[j]) {

Min_index = j;

}

}

if (I!=min_index) {

int temp = Arr[i];

Arr[i] = Arr[min_index];

Arr[min_index] = temp;

}

}

for (int i=0;i<arr.length;i++) {

System.out.print (arr[i]+ "");

}

} }

Java Array 5 jobs (2015-8-27)

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.