Classic array example and classic array example

Source: Internet
Author: User
Tags array example

Classic array example and classic array example

Example 1: average score of Students

1 public static void main (String [] args) {2 inputs input = new inputs (System. in); 4 int scores [] = new int [5]; 5 int sum = 0; 6 7 System. out. println ("Enter the score of five students:"); 8 for (int I = 0; I <scores. length; I ++) {// traverses the array 9 scores [I] = input. nextInt (); 10 sum + = scores [I]; // The score is accumulated 11} 12 System. out. print ("average score:" + sum/scores. length );

Example 2: output 5 shopping amounts and total amount

1 public static void main (String [] args) {2 inputs input = new inputs (System. in); 3 double sum = 0; 4 double scores [] = new double [5]; 5 for (int I = 0; I <scores. length; I ++) {6 System. out. print ("Enter the" + (I + 1) + "amount record:"); 7 scores [I] = input. nextDouble (); 8 sum + = scores [I]; 9} 10 System. out. println ("No. \ t" + "amount (RMB)"); 11 for (int I = 0; I <scores. length; I ++) {12 System. out. print (I + 1) + "\ t"); 13 System. out. println (scores [I]); 14} 15 System. out. println ("Total Amount: \ t" + sum );

Example 3: Enter the scores of five students cyclically, sort them in ascending order, and output the results.

1 public static void main (String [] args) {2 inputs input = new inputs (System. in); 3 int scores [] = new int [5]; 4 System. out. println ("Enter the score of five students:"); 5 // enter the score of 6 for (int I = 0; I <scores. length; I ++) {7 scores [I] = input. nextInt (); 8} 9 Arrays. sort (scores); // sort 10 systems. out. println ("student scores in ascending order:"); 11 for (int I = 0; I <scores. length; I ++) {12 System. out. println (scores [I] + ""); 13}

Example 4: Enter the score of the five students in this Java test on the keyboard to obtain the highest score.

1 public static void main (String [] args) {2 inputs input = new inputs (System. in); 3 int scores [] = new int [5]; 4 int max = 0; 5 System. out. println ("Enter the score of five students:"); 6 // enter the score 7 for (int I = 0; I <scores. length; I ++) {8 scores [I] = input. nextInt (); 9} 10 11 for (int I = 0; I <scores. length; I ++) {12 if (scores [I]> max) {13 max = scores [I]; 14} 15} 16 System. out. println ("highest score:" + max); 17}

Example 5: there is a group of students whose scores are {, 85, 60} in ascending order. To add a score for a student, insert it into the Score Sequence and maintain the ascending order.

1 public static void main (String [] args) {2 inputs input = new inputs (System. in); 3 int [] list = new int [6]; 4 list [0] = 99; 5 list [1] = 95; 6 list [2] = 92; 7 list [3] = 89; 8 list [4] = 69; 9 list [5] = 49; 10 int index = list. length; // Save the position of the new score 11 System. out. println ("Enter new score:"); 12 int num = input. nextInt (); // enter the data to be inserted 13 // locate the new element to insert 14 for (int I = 0; I <list. length; I ++) {15 16 if (num> list [I]) {17 index = I; 18 break; 19} 20} 21 // element moves 22 for (int I = list. length-1; I> index; I --) {23 list [I] = list [I-1]; // The element starting with index subscript is moved to a position 24} 25 list [index] = num; 26 System. out. println ("subscript of the inserted score:" + index); 27 28 System. out. println ("the inserted score is:"); 29 for (int I = 0; I <list. length; I ++) {30 System. out. println (list [I] + "\ t"); 31} 32}

Example 6: sort a group of unordered characters in ascending and reverse order.

1 public static void main (String [] args) {2 inputs input = new inputs (System. in); 3 String [] num = new String [] {"a", "c", "u", "B", "e", "p ", "f", "z"}; 4 System. out. print ("Original Character Sequence:"); 5 for (int I = 0; I <num. length; I ++) {6 System. out. print (num [I] + ""); 7} 8 Arrays. sort (num); 9 System. out. println (); // line feed 10 System. out. print ("sorted in ascending order:"); 11 for (int I = 0; I <num. length; I ++) {12 System. out. print (num [I] + ""); 13} 14 System. out. println (); // line feed 15 System. out. print ("output in descending order:"); 16 // in descending order, the first element is 17 for (int I = num. length-1; I> = 0; I --) {18 System. out. print (num [I] + ""); 19}

Example 7: Find the lowest mobile phone price and original location (subscript) of the four stores)

1 bytes input = new partition (System. in); 2 System. out. println ("Enter the prices of four stores"); 3 int [] num = new int [4]; 4 for (int I = 0; I <num. length; I ++) {5 System. out. print ("no." + (I + 1) + "Store Price:"); 6 num [I] = input. nextInt (); 7} 8 int min = num [0]; 9 int index = 0; 10 for (int j = 0; j <num. length; j ++) {11 if (num [j] <min) {12 min = num [j]; 13 index = j; 14} 15 16} 17 System. out. print ("lowest price:" + min); 18 System. out. println ("and its original position in the array (subscript) is:" + index); 19} 20}

Example 8: 10 integers are output from the keyboard. Valid values are 1, 2, and 3. Others are invalid and valid and invalid numbers are counted.

1 bytes input = new partition (System. in); 2 int nums [] = new int [10]; 3 int a = 0; 4 int B = 0; 5 int c = 0; 6 int d = 0; 7 System. out. println ("Enter 10:"); 8 for (int I = 0; I <nums. length; I ++) {9 nums [I] = input. nextInt (); 10 11 switch (nums [I]) {12 case a ++; 14 break; 15 case B ++; 17 break; 18 case :19 c ++; 20 break; 21 default: 22 d ++; 23 break; 24} 25 26} 27 System. out. println ("Number 1:" + a); 28 System. out. println ("Number 2:" + B); 29 System. out. println ("number 3:" + c); 30 System. out. println ("Number of invalid numbers:" + d );

Example 9: Suppose there is an array with a length of 5, int [] aray = new int [] {,-,-2}. First, create a new array, the storage order of the new array is required to be in reverse order with the elements of the original array. If the element value in the original array is smaller than 0, the new array should be stored with a value of 0,

1 bytes input = new partition (System. in); 2 3 int [] array = new int [] {1, 3,-1, 5,-2}; 4 System. out. println ("the original array is:"); 5 for (int I = 0; I <array. length; I ++) {6 System. out. print (array [I] + ""); 7} 8 System. out. println (); 9 int newarray [] = new int [5]; 10 for (int I = array. length-1; I> = 0; I --) {11 if (array [I] <0) {12 continue; 13} 14 if (array [I]> 0) {15 newarray [array. length-I-1] = array [I]; 16} 17} 18 System. out. println (""); 19 System. out. println ("the array after backward processing is:"); 20 for (int I = 0; I <newarray. length; I ++) {21 System. out. print (newarray [I] + ""); 22} 23}

Example 10:

1 public static void main (String [] args) {2 inputs input = new inputs (System. in); 3 String [] musics = new String [] {"Island", "Ocean", "Pretty", "Sun"}; 4 int index = musics. length; // Save the position where the new song is inserted. 5 // output the result before insertion. 6 System. out. print ("the array before insertion is:"); 7 for (int I = 0; I <musics. length; I ++) {8 System. out. print (musics [I] + ""); 9} 10 // new array 11 String [] newMusics = new String [musics. length + 1]; // new song array 12 String music = ""; // Save the song name entered by the user 13 // copy the elements in the array musics to the new song array newMusics 14 for (int I = 0; I <musics. length; I ++) {15 newMusics [I] = musics [I]; 16} 17 // enter the song name 18 System. out. print ("\ n enter the song name:"); 19 music = input. nextLine (); 20 // locate the insert position of the new element 21 for (int I = 0; I <musics. length; I ++) {22 if (musics [I]. compareToIgnoreCase (music)> 0) {23 index = I; 24 break; 25} 26} 27 // The element is removed after 28 for (int I = newMusics. length-1; I> index; I --) {29 newMusics [I] = newMusics [I-1]; // move the element starting with index subscript to a position 30} 31 newMusics [index] = music; // the position where the new element is located in index 32 System. out. print ("the array after reverse processing is:"); 33 for (int I = 0; I <newMusics. length; I ++) {34 System. out. print (newMusics [index] + ""); 35} 36 37} 38 39}

 

Related Article

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.