티스토리 뷰

 

배열의 저장된 5개의 실수를 생성후 최대값과 최소값을 구한후 출력하세요.

public class Test{

   public static void main(String[] args){

 

    double[] arr2 = {1.5,2.5,3.5,4.5,5.5};
     double max = arr2[0];
     double min = arr2[0];
     for (int i = 0 ; i < arr2.length ; i ++) {
       if(arr2[i] > max) {
         max = arr2[i];
       }else if( arr2[i] < max) {
         min = arr2[i];
       }
       
     }
     System.out.println(max);
     System.out.println(min);

 

    //Stream을이용한max,min
     System.out.println(Arrays.stream(arr2).max().getAsDouble());
     System.out.println(Arrays.stream(arr2).min().getAsDouble());

 

   }

}
     
     -Console-

5.5
1.5
5.5
1.5