Output of the Java programming exercise top three test scores

Source: Internet
Author: User

In the MU class network when learning to encounter such a Java programming exercises, just to learn the basic knowledge of Java to check:

According to what you have learned, write a Java program that achieves the top three requirements for output test scores: 1 test scores have been saved in the array scores, the array elements are 89, 23, 64, 91, 119, 52, 732 require a custom method to achieve the score ranking and output operations, the result array Pass in 3 as a parameter to determine the validity of the score (0-100), if the result is invalid, the result is ignored

I analyzed the process of this program myself:

(1) The first is to define a method that contains an integer array parameter, to receive an array of scores, to sort the scores and to output the top three

The specific steps are:

a Use the sort () method of the arrays class in the method to sort the array, by default in ascending order, and note the arrays class's use need to import;

The import java.util.Arrays of array class is introduced;

b Since only the first three of the test results need to be output, define a variable to count the number of the top three of the effective score;

C uses the For loop to iterate through the elements in the array, since the first three results are to be output, so we traverse backwards, that is, the reverse traversal;

for (int i = scores.length-1; I >= 0; i--) {}

D in the For loop to determine the validity of the results, if the score is less than 0 or greater than 100, then use continue ignore this score;

e If the result is valid, the number of effective results plus 1. Determine the number of effective results, if the number of effective scores equals 3, then the end of the cycle, only the results of the output

Top three;

(2) Complete the Main method

A define an array of scores scores, save the scheduled test scores 89,-23, 64, 91, 119, 52, 73;

b Call the custom method, pass in the score array, complete the output of the result;

The specific implementation code is:

Introduce array class import Java.util.arrays;public class Test {        //Finish Main method public    static void Main (string[] args) {       //Create Instance object of the HelloWorld class       test test=new Test ();   Define an integer array       int[] scores={89,-23,64,91,119,52,73,-45,78,168};       System.out.println ("Top three of the exam results:");   Call the method of the class, output the result       Test.result (scores);            }        Define the method to finish sorting and output the top three functions public    void result (int[] scores) {//Use the sort () method of the array        Arrays.sort (scores);//Initialize variable        int count=0;        for (int i=scores.length-1;i>=0;i--) {//If the entry loop is not a valid score, skip this score            if (scores[i]<0| | SCORES[I]>100) {                continue;            }            count++;            System.out.println (Scores[i]);//cumulative output of the number of points is 3 o'clock, abort the loop            if (count==3) {break                ;            }    }}
Results of the output:







Output of the Java programming exercise top three test scores

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.