강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

uri님의 프로필 이미지
uri

작성한 질문수

for문 반복문(2) - NO만 출력되는 문제

해결된 질문

작성

·

195

0

using System;
using System.Linq;

namespace Exam8_9
{
    internal class Calender
    {
        // endDate 필요 => 31,30,28
        // 1월부터 12월
        int[] Month31 = new int[] { 1, 3, 5, 7, 8, 10, 12 };
        int[] Month30 = new int[] { 4, 6, 9, 11 };
        int CurrentDay;

        public void test()
        {
            for(int i = 0; i < 13; i++)
            {
                PrintHeader(i);
                PrintMonth(i);
                PrintFooter();
            }
        }

        private void PrintHeader(int month)
        {
            Console.WriteLine($"{month}월");
            Console.WriteLine("-------------------------");
            Console.WriteLine(string.Format("{0,-4}{1,-4}{2,-4}{3,-4}{4,-4}{5,-4}{6,-4}",
                "일", "월", "화", "수", "목", "금", "토"));
        }

        private void PrintFooter()
        {
            Console.WriteLine();
            Console.WriteLine("-------------------------");
            Console.WriteLine();
        }

        public void PrintMonth(int month)
        {
            int endDate;
            if (Month31.Contains(month)) endDate = 31;
            else if (Month30.Contains(month)) endDate = 30;
            else endDate = 28;
            // month 를 출력
            for(int i=1; i<=endDate; i++)
                PrintDate(i);
        }

        public void PrintDate(int date)
        {   
            Console.Write(string.Format("{0, -5:NO}",date));
           /*  CurrentDay++;
             if(CurrentDay == 7)
             {
                 CurrentDay = 0;
                 Console.WriteLine();
             }*/
        }
    }
}

스크린샷 2023-10-31 034519.png

===================================================================

이런 식으로 수업과 똑같이 따라한다고 했지만, NO만 출력이 되네요. 문제가 뭔지 ChatGPT등을 통해서 찾아보고 있지만, 며칠째 도저히...답변이 달린다면 정말 감사드리고, 달리지 않는다고 해도 제 나름대로 계속 찾아보겠습니다.

쉽고 간단한 코드를 짤 때 어떻게 접근해야 할 지 가르쳐주셔서 수업 잘 듣고 있습니다. 감사합니다.

답변 1

0

안녕하세요 euan_um 님, 인프런 AI 인턴이에요.

코드를 살펴보니 PrintDate 메소드에서 출력시 사용하는 string.Format("{0, -5:NO}",date) 부분이 문제입니다. 여기서 :NO는 숫자를 출력할 때 천 단위마다 콤마(,)를 추가하여 표시하도록 지정하는 형식입니다. 하지만 숫자 자리를 비워두고 “NO”를 출력해야 할 것 같습니다. 따라서 이 부분을 수정해야 합니다.

아래의 코드로 PrintDate 메소드를 변경해주세요.

public void PrintDate(int date)
{
    Console.Write(string.Format("{0, -5}", "NO"));
    /*  CurrentDay++;
        if(CurrentDay == 7)
        {
            CurrentDay = 0;
            Console.WriteLine();
        }*/
}

위의 코드 변경 후에는 “NO”만 출력되는 문제가 해결되어 원하시는 출력 결과를 얻을 수 있을 것입니다. 진행하시는 데 도움이 되었으면 좋겠습니다. 추가로 궁금한 점이 있으면 언제든지 물어보세요. 감사합니다!

uri님의 프로필 이미지
uri

작성한 질문수

질문하기