C#/수업 내용

[C#] 산술 연산자

JSH1 2021. 8. 18. 16:17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("첫번째 숫자를 입력하세요: ");
            int a = Convert.ToInt32(Console.ReadLine());

            Console.Write("두번째 숫자를 입력하세요: ");
            int b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("--------------------------------");
            Console.WriteLine("{0}+{1}={2}", a, b, a + b);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("첫번째 숫자를 입력하세요: ");
            float a = Convert.ToSingle(Console.ReadLine());

            Console.Write("두번째 숫자를 입력하세요: ");
            int b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("--------------------------------");
            Console.WriteLine("{0}+{1}={2}", a, b, a + b);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("첫번째 숫자를 입력하세요: ");
            int a = Convert.ToInt32(Console.ReadLine());

            Console.Write("두번째 숫자를 입력하세요: ");
            int b = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("--------------------------------");
            Console.WriteLine("{0}+{1}={2}", a, b, a + b);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            //산술 연산자 ++, --
            //++ : 증가 연산자
            //-- : 감소 연산자
            //피연산자를 1씩 증가 시키거나 감소 시킨다
            int i = 3;
            Console.WriteLine(i); // 3
            Console.WriteLine(i++); // 3
            Console.WriteLine(i); // 4

            float j = 1.5f;
            Console.WriteLine(j); // 1.5
            Console.WriteLine(++j); // 2.5
            Console.WriteLine(j); // 2.5
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            // 산술 연산자(+, -, *, /, %)
            int max_hp = 100;
            int hp = max_hp;
            Console.WriteLine("{0}/{1}", hp, max_hp);

            Console.WriteLine("오크전사를 만났습니다.");
            int orcDamage = 13;
            Console.WriteLine("오크전사가 당신을 {0}만큼 쎄게 때렸습니다.", orcDamage);

            // hp = hp -orcDamage;
            // 복합할당식
            hp -= orcDamage;
            Console.WriteLine("{0}/{1}", hp, max_hp);

            Console.WriteLine("체력 물약을 사용했습니다.");
            //hp = hp + 5;
            //복합 할당식
            hp += 5;
            Console.WriteLine("{0}/{1}", hp, max_hp); // 92/100

            Console.WriteLine("오크전사가 당신에게 치명타 공격을 했습니다.");
            //오크전사의 치명타 공격력: 오크전사 공격력 * 2
            //hp=hp-orcDamage *2;
            //복합 할당식
            hp -= orcDamage * 2;
            Console.WriteLine("{0}/{1}", hp, max_hp); // 66/100

            float hp_percent = (float)hp / max_hp * 100;
            Console.WriteLine("{0}%", hp_percent); // 66%

            int weaponDamage = 5; // 나의 무기 공격력

            // % 2로 나눠 떨어지면 짝수
            // (주사위 눈:6) 주사위를 굴려서 짝수가 나오면 강화 성공
            Console.Write("주사위를 굴려주세요: ");
            string input = Console.ReadLine();

            // 형식 변환 string -> int (숫자 형식의 문자열) ex) "12"
            int dice_num = Convert.ToInt32(input);
            bool success = (dice_num % 2) == 0;

            Console.WriteLine("강화 성공 여부: {0}", success);
            // 만약에 성공했다면 무기공격력을 1 증가 시킨다
        }
    }
}


 

'C# > 수업 내용' 카테고리의 다른 글

[C#] if  (0) 2021.08.19
[C#] 논리 연산자  (0) 2021.08.19
[C#] ConsoleKeyInfo, 키 입력  (0) 2021.08.18
2021-08-18 오전  (0) 2021.08.18
2021-08-17 2  (0) 2021.08.17