.NET
C# 만나이 계산 방법 (chatGPT)
stacknstack
2023. 8. 1. 22:00
728x90
반응형
currentDate.Year와 birthDate.Year를 사용하여 현재 연도와 생년 연도를 가져온 다음, 두 가지 값을 빼서 연령을 계산한다. 그런 다음 생일이 지났는지 체크해서 연을 조정한다.
using System;
class Program
{
static void Main(string[] args)
{
// 현재 날짜
DateTime currentDate = DateTime.Now;
// 생년월일 입력
Console.WriteLine("생년월일을 입력하세요 (예: 1990-01-01): ");
DateTime birthDate = DateTime.Parse(Console.ReadLine());
// 만 나이 계산
int age = currentDate.Year - birthDate.Year;
// 생일이 지났는지 체크
if (currentDate.Month < birthDate.Month || (currentDate.Month == birthDate.Month && currentDate.Day < birthDate.Day))
{
age--;
}
// 결과 출력
Console.WriteLine("만 나이: " + age);
}
}
출처 | chatGPT
728x90
반응형