Enter the year and month of the Christian era and write a program to find the number of days in that month. In that case, you need to consider the leap year.
Leap years are determined by the following criteria.
① If the year is divisible by 4, it is a leap year. (2) However, as an exception, if the year is divisible by 100, it is not a leap year. ③ However, as an exception, if it is divisible by 400, it is a leap year.
In other words, the year 2000 is a leap year, and the year 2100 is not a leap year.
** Output example ** February 1990 => "February 1990 has 28 days" February 2000 => "February 2000 has 29 days" February 2100 => "February 2100 has 28 days" March 2000 => "March 2000 has 31 days"
** Model answer **
def get_days(year, month)
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if month == 2
if year % 4 == 0
if year % 100 == 0 && year % 400 != 0
days = 28
else
days = 29
end
else
days = 28
end
else
days = month_days[month - 1]
end
return days
end
puts "Please enter the year:"
year = gets.to_i
puts "Please enter the month:"
month = gets.to_i
days = get_days(year, month)
puts "#{year}Year#{month}The moon#{days}There are days"
** Explanation ** Except for February, the number of days in each month is fixed. Therefore, first write the code as follows.
def get_days(year, month)
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] #The number of days in each month is managed by an array
return month_days[month - 1]
end
puts "Please enter the year:"
year = gets.to_i
puts "Please enter the month:"
month = gets.to_i
days = get_days(year, month)
puts "#{year}Year#{month}The moon#{days}There are days"
Then, only in February, the conditional expression is used for output.
def get_days(year, month)
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if month == 2 #In February
#Substitute 29 for days in leap years
#Otherwise, substitute 28 for days
else
days = month_days[month - 1]
end
return days
end
puts "Please enter the year:"
year = gets.to_i
puts "Please enter the month:"
month = gets.to_i
days = get_days(year, month)
puts "#{year}Year#{month}The moon#{days}There are days"
Determine if it is a leap year. There were three leap year conditions, but they can be summarized as follows.
① That year is divisible by 4 ② However, if the year is divisible by 100 and not divisible by 400, it is not a leap year.
Therefore, the following conditional branches can be provided. [Example]
if year % 4 == 0 #The year is divisible by 4
if year % 100 == 0 && year % 400 != 0 #If the year is divisible by 100 and not divisible by 400
#Not a leap year
else
#leap year
end
end
If you reflect the above logic
def get_days(year, month)
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if month == 2
if year % 4 == 0 #The year is divisible by 4
if year % 100 == 0 && year % 400 != 0 #If the year is divisible by 100 and not divisible by 400
days = 28 #Not a leap year
else
days = 29 #leap year
end
else
days = 28 #Not a leap year
end
else
days = month_days[month - 1]
end
return days
end
puts "Please enter the year:"
year = gets.to_i
puts "Please enter the month:"
month = gets.to_i
days = get_days(year, month)
puts "#{year}Year#{month}The moon#{days}There are days"
Recommended Posts