• 회원가입
  • 로그인
  • 구글아이디로 로그인

[howto] C# - 사용자 입력값 더하기 ★ (Add Two Numbers)

4,273  

※ 예제2처럼, 사용자 입력값 받아 처리하는 게 훨씬 프로그램이 유용.


[예제1] 고정 숫자로 덧셈 연산.

 

using System;

namespace Homzzang

{

  class Program

  {

    static void Main(string[] args)

    {

      int x = 3;

      int y = 4;

      int sum = x + y;

      Console.WriteLine(sum); // 7

    }

  }

}

 

결과값: 7


[예제2] 사용자 입력값 받아 덧셈. 

 

using System;

namespace Homzzang

{

    class Program

    {

        static void Main(string[] args)

        {

            int x, y, sum; 

            Console.WriteLine("숫자 입력:");

            x = Convert.ToInt32(Console.ReadLine()); 

            Console.WriteLine("숫자 입력:");

            y = Convert.ToInt32(Console.ReadLine()); 

            sum = x + y;

            Console.WriteLine(sum);

        }

    }

}

 

결과값: 사용자 입력값에 따라 다름.

cf. (C++Java) 경우, 사용자 입력값 받아 더하기.



분류 제목
method C# - Methods (메서드) - 정의/호출
method C# - Method Parameters (메서드 매개변수)
method C# - Method Overloading (메서드 오버로딩)
class C# - OOP (객체 지향 프로그래밍)
class C# - Class (클래스) / Object (객체)
class C# - Class Members (클래스 멤버) - 속성(=필드), 메서드
class C# - Constructors (생성자)
class C# - Access Modifiers (접근 수정자)
class C# - Encapsulation (캡슐화) ※ Getter (게터) / Setter (세터)
class C# - Inheritance (상속)
class C# - Polymorphism (다형성)
class C# - Abstraction (추상화)
class C# - interface (인터페이스) ★
class C# - enums (이넘) - 상수 열거형 클래스
class C# - Files (파일) - 파일생성/파일읽기/파일쓰기
class C# - Exceptions (= Try...Catch..) - 에러 제어
howto C# - 사용자 입력값 더하기 ★ (Add Two Numbers)
2/2
목록
찾아주셔서 감사합니다. Since 2012