학습일자 : 2023.03.27


콘솔의 입출력

콘솔 출력

Console.WriteLine : 콘솔에 출력하고 줄 바꿈

Console.Write : 콘솔에 출력하고 줄을 바꾸지 않음

콘솔 입력

Console.ReadLine : 콘솔을 통해 한줄 입력받음

Console.ReadKey : 콘솔을 통해 키 입력받음

[콘솔 입출력 실습]

이름을 입력 받고, 환영 인사 후 본인이 마스터임에 놀라는 것이 1초마다 출력

internal class StartConsole
{
    private static int _index = 0;
    private static string _printText;

    public void ReadAndWrite()
    {
        Console.WriteLine("who?"); //글자 입력
        var val = Console.ReadLine(); //엔터 입력할 때까지의 값을 입력 받음 / Console.Read() : 한글자만 입력 받음

        Console.WriteLine($"안녕하세요! 마스터님!!");

        //Console.WriteLine($"예?!?!?! 저는 그저 말하는 {val}인데요?!?!?!?");

        System.Timers.Timer timer = new System.Timers.Timer(1000);
        
        timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        timer.AutoReset = true;
        timer.Enabled = true;

        for (; ; )
        {
            _printText = GetPrintText($"저는 그저 말하는 {val}인데요?!?!?!?");

            if (_index == 3)
            {
                break;
            }

        }

        timer.Stop();
        timer.Dispose();
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        Console.WriteLine(_printText);
        _index++;
    }

    private string GetPrintText(string str)
	{
		switch (_index)
		{
            case 0: return "예?!?!?!";
            case 1: return str;
            default:return "";
    }
	}
}

Untitled

자료형

: 자료의 형태를 저장

자료형 종류