오늘은 switch~case문이에요.


using System;

namespace Test.Swirch.test1{

public class Calendar{

public bool IsYear(int year){

bool isCheck = false;


if (((year % 400 ==0) && (year % 100 != 0) )||(year % 400 ==0)){

isCheck = true; //400으로 나눈 나머지값이 0이거나 100으로 나눈나머지가 0이아닐경우 true로변경


}

return isCheck;

}

public int Day(int year,int month){

int toDay=0;


if (IsYear (year)){

swtich(month){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: toDay = 31; break;  //month값이 12일경우 toDay에 31을 대입하고 순환문 빠져나옴

case 4:

case 6:

case 9:

case 11: toDay = 30;break; //month값이 30일경우 toDay에 31을 대입하고 순환문 빠져나옴

case 2: toDay = 29; break; //month값이 29일경우 toDay에 31을 대입하고 순환문 빠져나옴

     }


}else{

swtich(month){

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: toDay = 31; break; 

case 4:

case 6:

case 9:

case 11: toDay = 30;break;

case 2: toDay = 28; break;

}


}

return toDay;

}

}

}


using System;

using Test.Swirch.test1;

namespace Test.Swirch.test2{

public class Program{

public static void Main(string[] args){

Console.WriteLine("년도와 달을 입력하세요");

Console.Write("년도: ");

int year = int.Parse(Console.ReadLine());

Console.Write("달: ");

int month = int.Parse(Console.ReadLine());

Calendar cal = new Calendar();

int day=Calendar.Day(year,month);

Console.WriteLine("{0}년 {1}월에는 {2}이 있습니다.",year,month,days);

}

}


} 





#c# #switch~case #if문 #switch문




+ Recent posts