Java Programming Basics Array Exercises

Source: Internet
Author: User

Through the above two studies, did a practice.

Exercise 1: Enter 10 numbers without sorting to find the maximum and secondary maximum values.

1  Packageexercise;2 ImportJava.util.Scanner;3  Public classSort {4 5      Public Static voidMain (string[] args) {6         //TODO Auto-generated method stubs7Scanner in =NewScanner (system.in);8System.out.println ("Please enter numbers and end with A-1 input:");9         intinput =in.nextint ();Ten         int[] numbers =New int[10];//define and create an array of 10 elements One         intMax = 0; A         intSEC = 0; -          while(Input! =-1)//Input Array -         { the              for(inti = 0;i < numbers.length;i++) -             { -Numbers[i] =input; -input =in.nextint (); +                 //System.out.print (Numbers[i] + ""); -             } +         } ASYSTEM.OUT.PRINTLN ("The element in the array is:"); at          for(intElement:numbers)//foreach statement output array -System.out.print (element + ""); -System.out.print ("\ n"); -max = Numbers[9]; -SEC = Numbers[8]; -         if(Numbers[0] > Numbers[1]) in         { -max = Numbers[0]; toSEC = Numbers[1]; +         } -         Else the         { *max = Numbers[1]; $SEC = Numbers[0];Panax Notoginseng         } -          for(inti = 2; I < numbers.length;i++) the         { +             if(Numbers[i] >max) A             { theSEC =Max; +Max =Numbers[i]; -             } $             Else $             { -                 if(Numbers[i] >sec) -                 { theSEC =Numbers[i]; -                 }Wuyi             } the         } -SYSTEM.OUT.PRINTLN ("Maximum number:" + Max + "followed by:" +sec); Wu     } -}

The result of the operation is:

Please enter numbers and end with A-1 input:
5 20 40 60 85 95 93 71 83 62-1
The elements in the array are:
5 20 40 60 85 95 93 71 83 62
The maximum number is: 95 followed by: 93

Java Programming Basics Array Exercises

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.