Skip to main content
added 1 character in body
Source Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469

I have madewritten an age calculator that takes a birth datebirthDate as input.

I'd like a general review of this. I'mI'm especially concerned about the message variable and the lines after the try-/catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); } Console.WriteLine("Exit? (y/n)"); string userValue = Console.ReadLine(); if (userValue == "y") { Environment.Exit(0); } } } } } 

I have made an age calculator that takes a birth date as input.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try-catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); } Console.WriteLine("Exit? (y/n)"); string userValue = Console.ReadLine(); if (userValue == "y") { Environment.Exit(0); } } } } } 

I have written an age calculator that takes a birthDate as input.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try/catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); } Console.WriteLine("Exit? (y/n)"); string userValue = Console.ReadLine(); if (userValue == "y") { Environment.Exit(0); } } } } } 
Removed some text
Source Link
IEatBagels
  • 12.7k
  • 3
  • 48
  • 99

I am a beginner and I have made a tinyan age calculator in C# that calculates your age when you specify your birthdate. I'm pretty sure it will look horrible to any developer out there, but hey, that's why I've posted ittakes a birth date as input.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try-catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); } Console.WriteLine("Exit? (y/n)"); string userValue = Console.ReadLine(); if (userValue == "y") { Environment.Exit(0); } } } } } 

I am a beginner and I have made a tiny calculator in C# that calculates your age when you specify your birthdate. I'm pretty sure it will look horrible to any developer out there, but hey, that's why I've posted it.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try-catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); } Console.WriteLine("Exit? (y/n)"); string userValue = Console.ReadLine(); if (userValue == "y") { Environment.Exit(0); } } } } } 

I have made an age calculator that takes a birth date as input.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try-catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); } Console.WriteLine("Exit? (y/n)"); string userValue = Console.ReadLine(); if (userValue == "y") { Environment.Exit(0); } } } } } 

I am a beginner and I have made a tiny calculator in C# that calculates your age when you specify your birthdate. I'm pretty sure it will look horrible to any developer out there, but hey, that's why I've posted it.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try-catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); }   Console.WriteLine("Exit? (y/n)");   string userValue = Console.ReadLine();   if (userValue == "y")   {   Environment.Exit(0);   } } } } } 

I am a beginner and I have made a tiny calculator in C# that calculates your age when you specify your birthdate. I'm pretty sure it will look horrible to any developer out there, but hey, that's why I've posted it.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try-catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); } Console.WriteLine("Exit? (y/n)"); string userValue = Console.ReadLine(); if (userValue == "y") { Environment.Exit(0); } } } } } 

I am a beginner and I have made a tiny calculator in C# that calculates your age when you specify your birthdate. I'm pretty sure it will look horrible to any developer out there, but hey, that's why I've posted it.

I'd like a general review of this. I'm especially concerned about the message variable and the lines after the try-catch statement.

namespace Age { class Program { static void Main(string[] args) { while (true) { try { Console.Write("Enter your birtdate: "); DateTime birthDate = DateTime.Parse(Console.ReadLine()); int Days = (DateTime.Now.Year * 365 + DateTime.Now.DayOfYear) - (birthDate.Year * 365 + birthDate.DayOfYear); int Years = Days / 365; string message = (Days >= 365) ? "Your age: " + Years + " years" : "Your age: " + Days + " days"; Console.WriteLine(message); } catch { Console.WriteLine("You have entered an invalid date.\n"); }   Console.WriteLine("Exit? (y/n)");   string userValue = Console.ReadLine();   if (userValue == "y")   {   Environment.Exit(0);   } } } } } 
Tweeted twitter.com/#!/StackCodeReview/status/446148778845016064
Added additional explanation from comments
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Added possibility to exit application
Source Link
Commodent
  • 425
  • 2
  • 4
  • 7
Loading
fixed code block, title, typo; edited tags
Source Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469
Loading
Source Link
Commodent
  • 425
  • 2
  • 4
  • 7
Loading