Programming Exercises (XXIII)

Claim:

1) is defined by the claims with reference band return a value to achieve.

2) parameters passed by the length of the array (e.g., a length of 8), the array-valued after method execution.

3) generated by a random number less than 100 (int) (Math.random () * 100).

NOTE: Math class is a class for performing arithmetic operations, Math.random () floating point numbers 0-1.

task:

1) is defined with reference band return a value, it outputs a random number to achieve the array.

  prompt:

  Returns the array of values ​​is defined with reference belt 1, is passed through the length of the array parameters, return value assignment

  2. Create an integer array of specified length

  3. Use for loop through the array, a random number generated by Math.random () and assigned to an array member

  4. Use the return-valued array

2) the completion of the main method

  prompt:

  1. Call an array of custom method, preservation method after the implementation of the return

  2. Use of Arrays.toString () converts a string array and an output, note introduced java.util.Arrays;

import java.util.Arrays;
public class HelloJava {
    public static void main(String[] args) {
        Hello HelloJava = new new HelloJava ();
         // call a method and returns the value stored in the variable 
        int [] = hello.getArray the nums (. 8 );      
         // array into a string and outputs 
        System.out.println (Arrays. toString (nums));
    }
    /*
     * Function: Create int array specified length, and generates a random number within the array 100 each element of the assignment
     * Definition of the array with a reference method with return value, parameters passed by the length of the array, the return assignment
     * / 
    Public  int [] getArray ( int length) {
         // int array definition specified length 
        int [] = the nums new new  int [length];   
         // loop through the array assignment 
        for ( int I = 0; I <length; I ++ ) {
 
            // generates a random number less than 100, and each member of the array assigned to 
            the nums [I] = ( int ) (Math.random () * 100 );
        }
        return the nums; // array after-valued 
    }
}

Operating results: (a random number of array elements)

[26, 36, 96, 55, 88, 96, 10, 26]

 

Claim:

1) exam scores have been stored in the array, the array elements were 89, -23,64,91,119,52,73.

2) requires listing and output operation implemented by a custom method results, the results array as a parameter.

3) determine the effectiveness of performance requirements (0-100), if the score is invalid, ignore this result.

operation result:

Test scores of the top three are:
91
89
73

task:

Method 1) defines an array of integers containing the parameters for receiving the results array, and outputs the sorted top three results for

  prompt:

  1. Sort Arrays class in the method () method to sort the array, in ascending order by default, attention needs to import using Arrays class java.util.Arrays

  Number 2. Since only output the top three test scores, so the definition of a variable, statistically valid results of the top three

  3. iterate for elements in the array, since the top three results is to be output, the forward traversal from, i.e. reverse traversal. Such as: for (int i = scores.length - 1; i> = 0; i--) {}

  4. The validity judgment results, if the results is less than 0 or greater than 100, the continue ignored Results

  5. If valid results, the number of effective results plus 1. Determining the number of effective results, if the number of effective achievement of greater than 3, the end of the cycle, only the output of the top three grades

2) the completion of the main method

  prompt:

  1) Define a score array of scores, save the scheduled test scores 89, -23,64,91,119,52,73

  2) call a custom method, passing an array of achievements, completion

import java.util.Arrays;
public class HelloJava {
    public static void main(String[] args) { //完成 main 方法
        int[] scores={89,-23,64,91,119,52,73};
        System.out.println ( "test scores of the top three are:" );   
        HelloJava hello = new HelloJava();
        hello.getArrays(scores);
    }   
    // definition method and outputs the sorted top three grades complete functionality 
    public  void getArrays ( int [] Scores) {
        Arrays.sort (Scores); // Arrays.sort (array names) The array is sorted in ascending 
        int scoreNum = 0; // definition of the number of valid results 
        for ( int I =-scores.length. 1; I> = 0 && scoreNum <. 3 ; i--) { // due to the output of the top three grades, it is necessary to traverse from the forward, reverse traversal i.e. 
            IF (scores [I] <0 || scores [I]> 100) { // Analyzing results is valid, if ineffective, use continue to ignore this performance 
                continue ;
            } else {
            scoreNum ++; // + 1'd score number effective 
            }
            System.out.println(scores[i]); 
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/animo-2020/p/12512223.html