码迷,mamicode.com
首页 > 编程语言 > 详细

数组中获取最大值和最小值

时间:2018-12-14 17:33:09      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:int   索引   数组   span   code   test   style   array   最大值   

/*
 * 数组获取最大值,最小值
 */
public class ArrayTest2 {
    public static void main(String[] args) {
        //定义1个数组
        int[] arr= {12,13,34,55,100};
        //定义参照物
        int max=arr[0];
        int min=arr[0];
        
        //遍历数组 从索引1开始遍历
        for(int x=1;x<arr.length;x++)
        {
            if(max<arr[x])
            {
                max=arr[x];
            }
        }
        System.out.println("数组中最大值是"+max);
        
        
        //获取数组中的最小值
        for(int y=1;y<arr.length;y++)
        {
            if(arr[y]<min)
            {
                min=arr[y];
            }
        }
        System.out.println("数组中最小值是"+min);
    }

}

 

数组中获取最大值和最小值

标签:int   索引   数组   span   code   test   style   array   最大值   

原文地址:https://www.cnblogs.com/long-holiday/p/10119688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!