본문 바로가기
Dev/Java

4. 최댓값 알고리즘 (Max Algorithm)

by vellahw 2022. 8. 8.
//[?] 주어진 데이터 중에서 가장 큰 값 구하기

/**
 * 최댓값 알고리즘(Max Algorithm): (주어진 범위 + 주어진 조건)의 자료들의 가장 큰 값
 */

public class MaxAlgorithm2 {
    public static void main(String[] args) {
        int max = Integer.MIN_VALUE; // Initilize 초기화 | 정수 형식의 데이터 중 가장 작은 값으로 초기화 ***

        int[] numbers = { -2, -5, -3, -7, -1 };

        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] > max) {
                max = numbers[i]; // MAX: 더 큰 값으로 할당
            }
        }

        System.out.println("최댓값:" + max);
    }    
}

출력값: 최댓값:-1

댓글