java数组获取最值

 1 import java.util.Random;
 2 
 3 /**
 4  * 获取最大值最小值
 5  * @author shaobn
 6  *
 7  */
 8 public class Test2 {
 9     public static void main(String[] args) {    
10         Random random = new Random();
11         int[] a = new int[10];
12         for (int i = 0; i < 10; i++) {
13             a[i] = random.nextInt(100)+1;
14             System.out.println(a[i]);
15         }
16         getMax(a);
17         getMin(a);
18     }
19     //最大值
20     public static void getMax(int a[]){
21         int temp = a[0];    
22         
23         for (int i = 0; i < a.length-1; i++) {
24             if(temp>a[i+1]){
25                 continue;
26             }
27             temp = a[i+1];
28         }
29         System.out.println("最大值是:"+temp);
30     }
31     //最小值
32     public static void getMin(int a[]){
33         int temp = a[0];
34         for (int i = 0; i < a.length-1; i++) {
35             if(temp<a[i+1])
36                 continue;
37             temp = a[i+1];
38         }
39         System.out.println("最小值是:"+temp);
40     }
41 }

 

posted @ 2015-09-24 10:50  邻家小书童  阅读(219)  评论(0编辑  收藏  举报