C# 기초

[유니티] C# 기초 (본격 프로그래밍 시작해보기)

유니티 게임 개발 2025. 1. 3. 17:48
{
    //// 1. 입력받은 데이터가 숫자인지 문자열인지 판단
    string input = Console.ReadLine();
    int x;
    bool isSuccess;

    isSuccess = int.TryParse(input, out x);

    if (isSuccess)
    {
        Console.WriteLine("숫자입니다.");
    }
    else
    {
        Console.WriteLine("문자열입니다.");
    }
}

{
    //// 2. 입력받은 데이터가 숫자인지 문자열인지 불리언인지 판단
    string input = Console.ReadLine();
    int x;
    bool y;
    bool isNum;
    bool isBool;

    isNum = int.TryParse(input, out x);
    isBool = bool.TryParse(input, out y);

    if (isNum)
    {
        Console.WriteLine("숫자입니다.");
    }
    else if (isBool)
    {
        Console.WriteLine("불리언 입니다.");
    }
    else
    {
        Console.WriteLine("문자열 입니다.");
    }
}

{
    // 3. 입력받은 데이터가 숫자라면 100보다 큰지 작은지 알려주는 프로그램 만들기
    string input = Console.ReadLine();
    int x;
    bool isSuccess = int.TryParse(input, out x);

    if (isSuccess)
    {
        if (x >= 100)
        {
            Console.WriteLine($"{x}는 100 보다 같거나 큰 수 입니다.");
        }
        else
        {
            Console.WriteLine($"{x}는 100 보다 작은 수 입니다.");
        }
    }
    else
    {
        Console.WriteLine("숫자가 아닙니다.");
    }
}

{
    // 4. 입력받은 데이터가 숫자라면 짝수인지 홀수인지 알려주는 프로그램 만들기
    string input = Console.ReadLine();
    int x;
    bool isSuccess = int.TryParse(input, out x);

    if (isSuccess)
    {
        if (x % 2 == 0)
        {
            Console.WriteLine($"{x}은 짝수 입니다.");
        }
        else
        {
            Console.WriteLine($"{x}은 홀수 입니다.");
        }
    }
    else
    {
        Console.WriteLine("숫자가 아닙니다.");
    }
}

{
    // 5. 언제 if 를 쓰고 언제 case를 쓸까요?
    // 경우의 수가 많을 때 case를 쓴다.
}