Leap Year?

bmoore

Member
Joined
Feb 1, 2010
Messages
6
Programming Experience
Beginner
bare with me, I'm a beginner but what would "year" = ?

VB.NET:
 Sub Main()
        Dim month, year As Integer
        Console.WriteLine("Enter a month number:")
        month = Convert.ToInt32(Console.ReadLine)
        year = ? 
        Select Case month
            Case 1, 3, 5, 7, 8, 10, 12
                Console.WriteLine("There are 31 days in this month!")
            Case 4, 6, 9, 11
                Console.WriteLine("There are 30 days in this month!")
            Case 2
                Select Case year
                    Case 2004, 2008, 2012, 2016
                        Console.WriteLine("There are 29 days in this month!")
                    Case Else
                        Console.WriteLine("There are 28 days in this month!")


                End Select

        End Select
 
Note: I have used the TryParse() method to convert the user's string input into a number. The Convert method will fail if the user enters nothing or a non-numeric character. You also will have to test for the correct range of both inputs, which I have not included. The year needs to be 4 digits.

VB.NET:
    Sub Main()
		Dim month, year As Integer, snum As String
		Console.Write("Enter a month number:  ")
		snum = (Console.ReadLine)
		Integer.TryParse(snum, month)
		Console.Write("Enter a year number:  ")
		snum = (Console.ReadLine)
		Integer.TryParse(snum, year)
		Select Case month
			Case 1, 3, 5, 7, 8, 10, 12
				Console.WriteLine("There are 31 days in this month!")
			Case 4, 6, 9, 11
				Console.WriteLine("There are 30 days in this month!")
			Case 2
				If DateTime.IsLeapYear(year) Then
					Console.WriteLine("There are 29 days in this month!")
				Else
					Console.WriteLine("There are 28 days in this month!")
				End If
		End Select
	End Sub
 
Last edited:
Back
Top