java通过静态内部类获取随机生成数组中最大值和最小值

//在主函数中生成一个double数组,在类中对其最大值最小值进行筛选,然后通过静态内部类中的get方法获取到最大最小值,完全的面向对象考虑。

package static_class;

class MaxMin{
	public static class Result{
		private double min;
		private double max;
		
		public Result(double maax, double miin) {
			// TODO Auto-generated constructor stub
			this.min = miin;
			this.max = maax;
		}
		public double getMin() {
			return min;
		}
		public double getMax() {
			return max;
		}
		
	}
	
	public static Result getResult(double[] ary) {
		// TODO Auto-generated method stub
		double min = Double.MAX_VALUE;
		double max = Double.MIN_VALUE;
		for(double d : ary){
			if(d > max){
				max = d;
			}else if(d <= min){
				min = d;
			}
		}
		return new Result(max, min);
	}
}

public class test {

	public static void main(String[] args) {
		double[] ary = new double[5];
		for(int i = 0; i < 5; i++){
			ary[i] = 100*Math.random();
		}
		System.out.println("Original array is: ");
		for(double d : ary){
			System.out.println(d);
		}
		System.out.println("The biggest in ary is: " + MaxMin.getResult(ary).getMax());
		System.out.println("The smallest in ary is: " + MaxMin.getResult(ary).getMin());
	}
}

猜你喜欢

转载自blog.csdn.net/small__snail__5/article/details/81262131
今日推荐