본문 바로가기

프로그래밍/알고리즘(자바)

최대값과 최소값 Level 1

최대값과 최소값 Level 1

https://programmers.co.kr/learn/challenge_codes/125

public class GetMinMaxString {
    public String getMinMaxString(String str) {
        String[] spritString = str.split(" ");
        int min ,max;
        min = max = Integer.parseInt(spritString[0]); //초기값 주는 방식에 유의
        for(String val : spritString){                //for each문 구현
            int num = Integer.parseInt(val);
            if(num<min){                              // 삼항 연산자 불가 
                min = num;
            }else if(num>max){
                max = num;
            }
        }

        return (min + " " + max);
    }

    public static void main(String[] args) {
        String str = "1 2 3 4";
        GetMinMaxString minMax = new GetMinMaxString();
        //아래는 테스트로 출력해 보기 위한 코드입니다.
        System.out.println("최대값과 최소값은?" + minMax.getMinMaxString(str));
    }
}