An instance of getting the maximum and minimum values in an array
2017-09-12 19:14
537 查看
1.Writing code :
2.The program runs :
3.Program description :
3.1 The fifth line declares the integer variable "i" as the loop control variable and the index of the array;also,declare the variable min and the maximum variable Max that holds the minimum value.
3.2 The sixth line declares the integer array "a",whose array elements have a value of 23,1,43,5,70,32,55,76 and 234.
3.3 The seventh line set the initial value of min and max to the first element of the array.
3.4 Line 8~16 outputs the contents of the array one by one , and judges the maximum and the minimum values in the array.
3,5 Line 17~19 outputs the maximum and minimum values after comparison , After setting the initial values of the variables min and max to the first element of the array , then compare them to each of the elements in the array one by one . Smaller than
the min specifies the value of the element to the min to keep the content of the min minimum , when the element is larger than max , specify the value of the element to the max to store , and keep the contents of the maximum . When the for loop is executed
, it means that all the elements in the array have been compared . At this point , the contents of the variables min and max are the minimum and maximum.
package com.nuist.One; public class ArrayTwo { public static void main(String[] args) { int i,max,min; int [] a = {23,1,43,5,70,32,55,76,234}; max=min=a[0]; System.out.print("数组中的元素包括:"); for(i = 0;imax) max = a[i]; if(a[i]
2.The program runs :
数组中的元素包括:23 1 43 5 70 32 55 76 234 数组的最大值是:234 数组的最小值是:1
3.Program description :
3.1 The fifth line declares the integer variable "i" as the loop control variable and the index of the array;also,declare the variable min and the maximum variable Max that holds the minimum value.
3.2 The sixth line declares the integer array "a",whose array elements have a value of 23,1,43,5,70,32,55,76 and 234.
3.3 The seventh line set the initial value of min and max to the first element of the array.
3.4 Line 8~16 outputs the contents of the array one by one , and judges the maximum and the minimum values in the array.
3,5 Line 17~19 outputs the maximum and minimum values after comparison , After setting the initial values of the variables min and max to the first element of the array , then compare them to each of the elements in the array one by one . Smaller than
the min specifies the value of the element to the min to keep the content of the min minimum , when the element is larger than max , specify the value of the element to the max to store , and keep the contents of the maximum . When the for loop is executed
, it means that all the elements in the array have been compared . At this point , the contents of the variables min and max are the minimum and maximum.
相关文章推荐
- 计算数组中最大值、最小值和平均值的方法(The methods of calculating the maximum, minimum and average of a array )
- struts2 <debug>标签报错:Caught an exception while getting the property values of null
- An easy instance of using the template class in C++
- - No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing in
- 在C++中怎样获得数组的大小?(原文标题:How Would You Get the Count of an Array in C++?)
- Ruby Arrays: keep repeated values only from the end of an array of arrays
- Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10)
- The study of Class and Instance in Python(20170918)
- 【特别好】【位运算】maximum-xor-of-two-numbers-in-an-array
- Compute the maximal length of the increasing subsequence in an array
- Teddy's Aspect Weaver Version 0.3 with Great Updating and Fixing, Especially the Implementing of Getting Runtime Method Context Info and Method Arguments in MSIL Level
- Given an array of size N in which every number is between 1 and N, determine if there are any dupli
- (NOT CLEAR)Question 40: In the declaration below, p is a pointer to an array of 5 int pointers:
- Filter (seach and replace) array of bytes in an InputStream
- How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)
- Each child in an array or iterator should have a unique "key" prop. Check the render method of `Tabl
- An Overview Of The New Services, Controls, And Features In ASP.NET 2.0
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- 前缀树及应用两数异或Leetcode之 Maximum XOR of Two Numbers in an Array
- How Would You Get the Count of an Array in C++?