카테고리 없음

[알고리즘]자료형 확인하는 방법_java(해커랭크_Java Int to String)

seulhasony 2024. 1. 26. 12:57

문제는 int형을 string으로 형변환하고 string으로 잘 변형되었는지 확인하는 로직을 짜서 원하는 출력값을 출력하면 되는 것이다.

https://www.hackerrank.com/challenges/java-int-to-string/problem?isFullScreen=true

 

Java Int to String | HackerRank

Convert an integer to a string.

www.hackerrank.com

 

솔루션

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num = scan.nextInt();
        String str = Integer.toString(num);
        
        if(str instanceof String){
            System.out.println("Good job");    
        }else{
            System.out.println("Wrong answer");    
        }
        
    }
}

 

쉬운 문제였지만, instanceof를 오랜만에 사용해서 다시금 상기시키고자 포스팅해보았습니당