Well off with you to learn JAVA -------- get the maximum and minimum values in the array instance, java -------- Array

Source: Internet
Author: User

Well off with you to learn JAVA -------- get the maximum and minimum values in the array instance, java -------- Array

Today, we will share with you how to obtain the maximum and minimum values in the array.

Example: TestJava4_3.java

01 // This program is mainly used to obtain the maximum and minimum values in the array.

02 public class TestJava4_3

03 {

04 public static void main (String args [])

05 {

06 int I, min, max;

07 int A [] = {, 62}; // declare an integer array A and assign the Initial Value

08

09 min = max = A [0];

10 System. out. print ("elements of array A include :");

11 for (I = 0; I <A. length; I ++)

12 {

13 System. out. print (A [I] + "");

14 if (A [I]> max) // determines the maximum value.

15 max = A [I];

16 if (A [I] <min) // judge the minimum value

17 min = A [I];

18}

19 System. out. println ("the maximum value of \ n array is:" + max); // The maximum value of output

20 System. out. println ("the minimum value of the array is:" + min); // the minimum value of the output

21}

22}

 

Output result:

Elements of array A include: 74 48 30 17 62

The maximum value of the array is 74.

The minimum value of the array is 17.

 

Program description:

1. Row 6th declares the integer variable I as the index of the loop control variable and array: it also declares the variable max that stores the min and max values of the minimum values.

2. Row 7th declares an integer array A. the array has five elements whose values are 74, 48, 30, 17, and 62, respectively.

3. Set the initial values of min and max on Line 1 to the first element of the array.

4. 10th ~ The 18 rows output the contents of the array one by one, and judge the maximum and minimum values of the array.

5. 19th ~ The maximum and minimum values after 20 rows of output comparison. Set the min variable and the max initial value to the first element of the array, and then compare them with the elements in the array one by one. Smaller than min, the value of this element is specified to min for storage, so that min content remains the minimum; similarly, when this element is larger than max, the value of this element is specified to be stored in max to maximize the content of max. After the for loop is executed, all the elements in the array have been compared. At this time, the min and max variables are the minimum and maximum values.


Java implements maximum and minimum values in the array.

Int minparam = 0, maxparam = 0;
For (int I = 0; I <a. length; I ++ ){
If (a [I] <= minparam ){
Minparam = a [I];
} Else if (a [I]> = maxparam ){
Maxparam = a [I];
}
}
System. out. println (maxparam-minparam );

Java array maximum and minimum values

Int max_idx = 0; // Number of subscripts with the highest score
Int min_idx = 0; // Number of subscripts with the lowest score

Int min = Integer. MAX_VALUE; // minimum value
Int max = Integer. MIN_VALUE; // maximum value
//////////////////////////////////////// //////////////////////////////
Int sum = 0; // total score
For (int I = 0; I <score. length; I ++ ){
System. out. print ("Enter the" + (I + 1) + "Student name :");
Name [I] = in. next ();
System. out. print ("Enter the" + (I + 1) + "Student's score :");
Score [I] = in. nextInt ();

If (score [I] <min ){
Min = score [I];
Min_idx = I;
}
If (score [I]> max ){
Max = score [I];
Max_idx = I;
}
Sum + = score [I];
}

System. out. printf ("minute: % d, highest score: % d, average score: % d", min, max, sum/score. length );

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.