본문 바로가기
Language/JAVA

[JAVA] while (n-- > 0) 의미

by 코젼 2024. 2. 11.
728x90
반응형

while 문에서 사용하는 while (n-- > 0)

n--; n > 0 를 합쳐놓은 형태이다.

 

▶️ 예제

배열 내에서 최댓값, 최솟값 구하는 예제

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main (String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int count = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());

        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        int num;

        while (count-- > 0) {
            num = Integer.parseInt(st.nextToken());
            min = Math.min(min, num);
            max = Math.max(max, num);
        }

        System.out.println(min + " " + max);
        br.close();
    }
}

 


▶️ 참고

https://java119.tistory.com/105

728x90
반응형

댓글