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

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;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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值