If you can't see Solution Explorer
Let's start the project :-)
First lines...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
Console.WriteLine("Hello ABC IT!");
Console.ReadKey(true);
}
}
}
Run (Start) project now again
Congratullations - you'v created your first program in C#
Create a program that can sum times - for example
2:37:52 + 0:44:18 = ?
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
var userInput = Console.ReadLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(userInput);
Console.ReadKey(true);
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var userInput = Console.ReadLine();
var timeParts = userInput.Split(':');
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Hour: {timeParts[0]} minut: {timeParts[1]} seconds: {timeParts[2]}");
Console.ReadKey(true);
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
while (true)
{
var userInput = Console.ReadLine();
var timeParts = userInput.Split(':');
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Hour: {timeParts[0]} minut: {timeParts[1]} seconds: {timeParts[2]}");
Console.ResetColor();
}
//Console.ReadKey(true);
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
while (true)
{
var userInput = Console.ReadLine();
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
var seconds = int.Parse(timeParts[2]);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Hour: {hours} minut: {minuts} seconds: {seconds}");
Console.ResetColor();
}
//Console.ReadKey(true);
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = new DateTime();
while (true)
{
var userInput = Console.ReadLine();
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
var seconds = int.Parse(timeParts[2]);
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
Console.ResetColor();
}
//Console.ReadKey(true);
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = new DateTime();
while (true)
{
var userInput = Console.ReadLine();
if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("... Press any key to close the window ...");
Console.ReadKey(true);
break;
}
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
var seconds = int.Parse(timeParts[2]);
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
Console.ResetColor();
}
//Console.ReadKey(true);
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = new DateTime();
while (true)
{
try
{
var userInput = Console.ReadLine();
if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("... Press any key to close the window ...");
Console.ReadKey(true);
break;
}
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
var seconds = int.Parse(timeParts[2]);
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
Console.ResetColor();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
// TODO : Display result anyway
}
}
//Console.ReadKey(true);
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = new DateTime();
while (true)
{
try
{
var userInput = Console.ReadLine();
if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("... Press any key to close the window ...");
Console.ReadKey(true);
break;
}
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
if (minuts > 59 || minuts < 0)
{
throw new Exception("The max value of minuts is 59");
}
var seconds = int.Parse(timeParts[2]);
if (seconds > 59 || minuts < 0)
{
throw new Exception("The max value of seconds is 59");
}
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
Console.ResetColor();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
// TODO : Display result anyway
}
}
//Console.ReadKey(true);
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = new DateTime();
while (true)
{
try
{
var userInput = Console.ReadLine();
if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("... Press any key to close the window ...");
Console.ReadKey(true);
break;
}
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
if (minuts > 59 || minuts < 0)
{
throw new Exception("The max value of minuts is 59");
}
var seconds = int.Parse(timeParts[2]);
if (seconds > 59 || minuts < 0)
{
throw new Exception("The max value of seconds is 59");
}
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
DisplayResult(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
DisplayResult(result);
}
}
//Console.ReadKey(true);
}
private static void DisplayResult(DateTime result)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
Console.ResetColor();
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = new DateTime();
while (true)
{
try
{
var userInput = Console.ReadLine();
if ("end".Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("... Press any key to close the window ...");
Console.ReadKey(true);
break;
}
result = CountResult(result, userInput);
DisplayResult(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
DisplayResult(result);
}
}
//Console.ReadKey(true);
}
private static DateTime CountResult(DateTime result, string userInput)
{
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
if (minuts > 59 || minuts < 0)
{
throw new Exception("The max value of minuts is 59");
}
var seconds = int.Parse(timeParts[2]);
if (seconds > 59 || minuts < 0)
{
throw new Exception("The max value of seconds is 59");
}
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
return result;
}
private static void DisplayResult(DateTime result)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
Console.ResetColor();
}
}
}
... now the EndProgram constant represents the string "end":
using System;
namespace AbcItNetFramework
{
internal class Program
{
private const string EndProgram = "end";
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = new DateTime();
while (true)
{
try
{
var userInput = Console.ReadLine();
if (EndProgram.Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("... Press any key to close the window ...");
Console.ReadKey(true);
break;
}
result = CountResult(result, userInput);
DisplayResult(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
DisplayResult(result);
}
}
//Console.ReadKey(true);
}
private static DateTime CountResult(DateTime result, string userInput)
{
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
if (minuts > 59 || minuts < 0)
{
throw new Exception("The max value of minuts is 59");
}
var seconds = int.Parse(timeParts[2]);
if (seconds > 59 || minuts < 0)
{
throw new Exception("The max value of seconds is 59");
}
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
return result;
}
private static void DisplayResult(DateTime result)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{result.Hour:00}:{result.Minute:00}:{result.Second:00}");
Console.ResetColor();
}
}
}
using System;
namespace AbcItNetFramework
{
internal class Program
{
private const string EndProgram = "end";
private static readonly DateTime startTime = new DateTime();
static void Main()
{
// Console.WriteLine("Hello ABC IT!");
Console.WriteLine("Welcome to the Time Counter. Please enter the time in HH:MM:SS format and pres ENTER...");
var result = startTime;
while (true)
{
try
{
var userInput = Console.ReadLine();
if (EndProgram.Equals(userInput, StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("... Press any key to close the window ...");
Console.ReadKey(true);
break;
}
result = CountResult(result, userInput);
DisplayResult(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
DisplayResult(result);
}
}
//Console.ReadKey(true);
}
private static DateTime CountResult(DateTime result, string userInput)
{
var timeParts = userInput.Split(':');
var hours = int.Parse(timeParts[0]);
var minuts = int.Parse(timeParts[1]);
if (minuts > 59 || minuts < 0)
{
throw new Exception("The max value of minuts is 59");
}
var seconds = int.Parse(timeParts[2]);
if (seconds > 59 || minuts < 0)
{
throw new Exception("The max value of seconds is 59");
}
result = result.AddHours(hours);
result = result.AddMinutes(minuts);
result = result.AddSeconds(seconds);
return result;
}
private static void DisplayResult(DateTime result)
{
var realResult = result - startTime;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"{Math.Floor(realResult.TotalHours):00}:{realResult.Minutes:00}:{realResult.Seconds:00}");
Console.ResetColor();
}
}
}