본문 바로가기
IT/C#

C# (배열을 이용하여 일주일 날씨 통계 출력)

by hjshims 2021. 5. 25.

 

 

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

namespace RoadBook.CsharpBasic.Chapter01.p0525
{
    class Ex002
    {
        public void Run()
        {
            string[] weathers = new string[7];

            weathers[0] = "suuny";
            weathers[1] = "suuny";
            weathers[2] = "rainy";
            weathers[3] = "cloudy";
            weathers[4] = "rainy";
            weathers[5] = "snow";
            weathers[6] = "suuny";

            int dayCnt = weathers.Length;

            int sunnyCnt = 0;
            int cloudyCnt = 0;
            int rainyCnt = 0;
            int snowCnt = 0;

            for(int idx=0; idx<dayCnt; idx++)
            {
                string weather = weathers[idx];

                if(weather == "sunny")
                {
                    sunnyCnt++;
                }
                else if (weather == "cloudy")
                {
                    cloudyCnt++;
                }
                else if (weather == "rainy")
                {
                    rainyCnt++;
                }
                else if (weather == "snow")
                {
                    snowCnt++;
                }
            }

            Console.WriteLine("맑음 : {0} / 흐림 : {1} / 비 : {2} / 눈 : {3}", sunnyCnt, cloudyCnt, rainyCnt, snowCnt);
        }
    }
}

 

<출력결과>