Top three output test scores for Java programming exercises

Source: Internet
Author: User
Tags array sort

Top three output test scores for Java programming exercises

When I was studying MOOC, I encountered such a Java programming exercise. I checked the Basic Java knowledge I learned:

Compile a Java program based on your knowledge to achieve the top three requirements for output test scores: 1. the test scores are saved in the array scores, with the array elements 89,-23, 64, 91,119, 52,732 require that you use a custom method to rank the score and output the operation. Pass the score array as a parameter. 3. determine the validity of the score (0-100). If the score is invalid, this score is ignored.

I analyzed the process of this program myself:

(1) first, define a method that contains the integer array parameters to receive the result array, sort the results, and output the top three

The specific steps are as follows:

A uses the sort () method of the Arrays class in the method to sort the array. By default, the array is sorted in ascending order. Note that the Arrays class needs to be imported;

// Introduce the array class import java. util. Arrays;

B. Because only the top three exam scores need to be output, a variable is defined to count the number of the top three exam scores;

C. Use the for loop to traverse the elements in the array. Since the top three scores are to be output, traverse from the back to the front, that is, traverse in reverse order;

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

D. Determine the score validity in the for loop. If the score is smaller than 0 or greater than 100, use continue to ignore the score;

E. If the score is valid, the number of valid scores increases by 1. Determine the number of valid scores. If the number of valid scores is 3, the cycle ends and only the scores are output.

Top 3;

(2) Complete the main method

A defines A score array scores and saves the expected exam scores 89,-23, 64, 91,119, 52, 73;

B. Call the custom method and input the score array to output the score;

The specific implementation code is:

// Introduce the array class import java. util. arrays; public class Test {// complete the main method public static void main (String [] args) {// create a HelloWorld class instance object Test = new test (); // define an integer array int [] scores = {89,-23,64, 91,119, 168,-45, 78,}; System. out. println ("the top three of the test scores are:"); // call the class method and output the result test. result (scores);} // define the method to sort the scores and output the top three functions public void result (int [] scores) {// use the array sort () method To sort Arrays. sort (scores); // initialization variable int count = 0; for (int I = scores. length-1; I> = 0; I --) {// if the cycle is not a valid score, skip this score if (scores [I] <0 | scores [I]> 100) {continue;} count ++; System. out. println (scores [I]); // if (count = 3) {break ;}}}}
Output result:

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.