본문 바로가기
c#/수업 내용

메서드 연습 문제

by 이지훈26 2021. 8. 24.

처음 오류 상황 ) = 형식 앞에 static을 안써서 1차적 오류가 났고, void본문에서 매개변수 summonsticket을 대문자로 써서 2차적 오류가 났었다.

 

 

using System;

namespace HelloWorld
{
    class Program
    {
        static int gold = 1000;
        static int diamond = 0;
        static int summonsticket = 0;

        static void Main(string[] args)
        {

            BuyDiamond();
            BuyDiamond();
            BuyDiamond();
            BuySummonsticket();
        }
        static void BuyDiamond()
        {
            Console.WriteLine("다이아몬드 1개를 구매 했습니다.");
            gold -= 100;
            diamond += 1;
            Console.WriteLine("골드:{0}, 다이아몬드:{1}, 소환권:{2}", gold, diamond, summonsticket);

        }
        static void BuySummonsticket()
        {
            Console.WriteLine("소환권을 1개 구매 했습니다.");
            gold -= 10;
            summonsticket += 1;
            Console.WriteLine("골드:{0}, 소환권:{1}, 소환권:{2}", gold, diamond, summonsticket);
        }
    }
}

 

---메서드 기능 생각---

골드 : 1000개
다이아몬드 : 0개
소환권: 0개


다이아몬드 1개를 구매할 때 골드 100개 사용

소환권 1개를 구매할 때 골드 10개 사용
호출
static

 

순서대로 풀었다. 무서운건 호출 전에 오류가 많이 나서 문제점(오류) 인지가 생각보다 어렵다. 오류가 나면 당황하고 지금 쓴것이 틀렸는지 다른걸 안써서 오류가 났는지 생각이 들어, 아예 전부를 지우고 다시 할까 고민도 들 때가 있다.

 

using System;

namespace HelloWorld
{
    class Program
    {
        static int marinedamage = 5;
        static int zugglingHp = 8;
        static void Main(string[] args)
        {
            Console.Write("마린의 공격력은 몇입니까? :");
            int marine1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("저글링의 체력은 몇입니까? :");
            int zuggling = Convert.ToInt32(Console.ReadLine());

            Attack1();
            Attack2();
            Attack3();
        }
        static void Attack1()
        {
            Console.WriteLine("마린이 저글링을 공격 했습니다.");
            marinedamage = 5;
            zugglingHp -= 5;
            Console.WriteLine("마린:{0}, 저글링{1}", marinedamage, zugglingHp);
        }
        static void Attack2()
        {
            Console.WriteLine("마린이 저글링을 공격 했습니다.");
            marinedamage = 5;
            zugglingHp -= 5;
            Console.WriteLine("마린:{0}, 저글링{1}", marinedamage, zugglingHp);
        }
        static void Attack3()
        {
            if (zugglingHp > 0)
            {
                zugglingHp -= marinedamage;
                Console.WriteLine("저글링의 체력:{0}", zugglingHp);
            }
            else if (zugglingHp > 0)
            {
                Console.WriteLine("저글링이 사망하였습니다. ", zugglingHp);
            }
            else
            {
                Console.WriteLine("잘못된 대상입니다. ");
            }
        }
    }
}

"저글링이 사망하였습니다" 가 출력이 안된다. 오류 찾기.

using System;

namespace HelloWorld
{
    class Program
    {
        static int marinedamage = 5;
        static int zugglingHp = 8;
        static void Main(string[] args)
        {
            Console.Write("마린의 공격력은 몇입니까? :");
            int marine1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("저글링의 체력은 몇입니까? :");
            int zuggling = Convert.ToInt32(Console.ReadLine());

            Attack1();
            Attack2();
            Attack3();
        }
        static void Attack1()
        {
            Console.WriteLine("마린이 저글링을 공격 했습니다.");
            marinedamage = 5;
            zugglingHp -= 5;
            Console.WriteLine("마린:{0}, 저글링{1}", marinedamage, zugglingHp);
        }
        static void Attack2()
        {
            Console.WriteLine("마린이 저글링을 공격 했습니다.");
            marinedamage = 5;
            zugglingHp -= 5;
            Console.WriteLine("마린:{0}, 저글링{1}", marinedamage, zugglingHp);
        }
        static void Attack3()
        {
            if (zugglingHp > 0)
            {
                zugglingHp -= marinedamage;
                Console.WriteLine("저글링의 체력:{0}", zugglingHp);
            }
            else if (zugglingHp < 0)
            {
                Console.WriteLine("저글링이 사망하였습니다. ", zugglingHp);
            }
            else
            {
                Console.WriteLine("잘못된 대상입니다. ");
            }
        }
    }
}

else if 에서 부등호를 반대로 써버렸다.