- package com.hbb;
- import java.util.Arrays;
- public class GetMaxAndMin {
- /**
- * 取出一个随机数组中的最大数和最小数
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] array = new int[5];
- for(int i=0; i<array.length; i++){
- array[i] = (int)(Math.random()*50);
- }
- // for(int j=0; j<array.length; j++){
- // System.out.println(array[j]);
- // }
- System.out.println(Arrays.toString(array));
- System.out.println(maxMin.getResult(array).getMax());
- System.out.println(maxMin.getResult(array).getMin());
- }
- }
- class maxMin {
- public static class Result{
- private int max;
- private int min;
- public Result(int max, int min){
- this.max = max;
- this.min = min;
- }
- public int getMax(){
- return max;
- }
- public int getMin(){
- return min;
- }
- }
- public static Result getResult(int[] array){
- int max = Integer.MIN_VALUE;
- int min = Integer.MAX_VALUE;
- for(int i:array){
- if(i>max){
- max = i;
- }
- if(i<min){
- min = i;
- }
- }
- return new Result(max,min);
- }
- }
复制代码
我这里可以运行,其中一次运行结果
[45, 24, 8, 8, 43]
45
8
|