-
Mar 28th, 2015, 07:17 AM
#1
Thread Starter
Fanatic Member
LYL Leep Year Lister.
Hi this is a small little console project to list leap years between two different dates.
Very simple to use enter a starting date and an ending date. Hope you find it us full.
csharp Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LYL
{
class Program
{
private static bool IsLeapYear(int year)
{
//Check for leap year.
if ((year % 4 == 0 && !(year % 100 == 0 && !(year % 400 == 0))))
return true;
return false;
}
static void Main(string[] args)
{
int _startY = 0;
int _endY = 0;
int I = 0;
Console.WriteLine("+------------------+");
Console.WriteLine("+ Leap Year Lister +");
Console.WriteLine("+ By Ben +");
Console.WriteLine("+------------------+");
try
{
Console.Write("Enter starting year: ");
_startY = int.Parse(Console.ReadLine());
Console.Write("Enter ending year: ");
_endY = int.Parse(Console.ReadLine());
}
catch
{
return;
}
//Print out years
for (I = _startY; I < _endY + 1; I++)
{
//Test for leap year.
if (IsLeapYear(I))
{
//Was leap year.
Console.WriteLine(I + " is a leap year");
}
else
{
//Was not a leap year
Console.WriteLine(I + " was not a leap year.");
}
}
}
}
}
-
Mar 30th, 2015, 10:40 AM
#2
Re: LYL Leep Year Lister.
I know this isn't code it better, but instead of using a try/catch to check whether a user has input a valid integer, you should really be using .TryParse. If, at any time, you can handle an error (and actually do something about it) without using an exception, you should.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|