您的位置:首页 > 产品设计 > UI/UE

An instance of getting the maximum and minimum values in an array

2017-09-12 19:14 537 查看
1.Writing code :

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐