티스토리 뷰

LANGUAGE/C#

[C#] 표준 입력

진심스테이크 2018. 6. 12. 22:03

 

표준 입력

- Console.Readkey( ) : 사용자가 눌린 키 한 문자 정보를 리턴하는 메소드

- 함수 원형 : 오버로딩

  public static ConsoleKeyInfo ReadKey( )

  public static ConsoleKeyInfo ReadKey(bool intercept) : true이면 화면 출력을 하지 않고, false이면 화면 출력

- ConsoleKeyInfo : 키의 문자와 shift, alt, ctrl 보조키 상태 포함

  - ConsoleKeyInfo 속성 : ConsolekeyInfo.Key, ConsoleKeyInfo.KeyChar, ConsoleKey.A, ConsoleKey.Escape 등

namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleKeyInfo keyInfo;
            do
            {
                keyInfo = Console.ReadKey();
                if (keyInfo.Key == ConsoleKey.A)  //대소문자를 구분하지 않음 - a, A둘다 눌렀을 때 if문 실행
                    Console.WriteLine("a가 눌렸다");
            } while (keyInfo.Key != ConsoleKey.Escape);
        }
    }
}
 
namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleKeyInfo keyInfo;
            do
            {
                keyInfo = Console.ReadKey(true);
                if (keyInfo.KeyChar == 'a')
                    Console.WriteLine("a가 눌렷다"); //'a'를 눌렸을때만 출력
                Console.Write(keyInfo.KeyChar);
            } while (keyInfo.Key != ConsoleKey.Escape);
        }
    }
}

 

 

- Console.ReadLine( ) : Enter키가 눌려질 때까지 입력 받은 문자열을 리턴하는 메소드

namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            int kor, eng, math, total;
            float avg;
            Console.Write("국어 점수 : ");
            kor = Convert.ToInt32(Console.ReadLine());
            Console.Write("영어 점수 : ");
            eng = Convert.ToInt32(Console.ReadLine());
            Console.Write("수학 점수 : ");
            math = Convert.ToInt32(Console.ReadLine());
            total = kor + eng + math;
            avg = total / 3.0f;
            Console.WriteLine("{0} {1}  {2} {3} {4}", kor, eng, math, total, avg);
        }
    }
}

'LANGUAGE > C#' 카테고리의 다른 글

[C#] 값 형식 / 참조 형식  (0) 2018.06.12
[C#] 사용자 지정형  (0) 2018.06.12
[C#] 변환  (0) 2018.06.12
[C#] 데이터형  (0) 2018.06.12
[C#] .NET FRAMEWORK  (0) 2018.06.12
댓글