mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
41 lines
917 B
C#
41 lines
917 B
C#
WriteLine("Before parsing");
|
|
Write("What is your age? ");
|
|
string? input = ReadLine(); // or use "49" in a notebook
|
|
|
|
try
|
|
{
|
|
int age = int.Parse(input!);
|
|
WriteLine($"You are {age} years old.");
|
|
}
|
|
catch (OverflowException)
|
|
{
|
|
WriteLine("Your age is a valid number format but it is either too big or small.");
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
WriteLine("The age you entered is not a valid number format.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteLine($"{ex.GetType()} says {ex.Message}");
|
|
}
|
|
WriteLine("After parsing");
|
|
|
|
Write("Enter an amount: ");
|
|
string amount = ReadLine()!;
|
|
if (string.IsNullOrEmpty(amount)) return;
|
|
|
|
try
|
|
{
|
|
decimal amountValue = decimal.Parse(amount);
|
|
WriteLine($"Amount formatted as currency: {amountValue:C}");
|
|
}
|
|
catch (FormatException) when (amount.Contains("$"))
|
|
{
|
|
WriteLine("Amounts cannot use the dollar sign!");
|
|
}
|
|
catch (FormatException)
|
|
{
|
|
WriteLine("Amounts must only contain digits!");
|
|
}
|