Conteúdo do Curso
Introduction to .NET with C#
2. Asynchronous Programming and Introduction to Web Services
3. Introduction to Databases with .NET
Introduction to .NET with C#
Task - Improving the Calculator
The code for the calculator which we created in the previous task is given below. Your task is to add checks to ensure proper user input.
- Add a check to make sure that the user enters appropriate input for the
opr
variable. The input should not be empty and should only be equal to+
,-
,/
or*
. In case of wrong input, it should prompt the user again for entry. Similar to point 1, add checks forfirst
andsecond
. Make sure the user enters a valid number.
There can be multiple ways to do this. As long as it satisfies the condition, it is a valid solution. Ideally there should be 3 separate try-catch statements for the 3 inputs. You can put all the try-catch statements in a single loop, or separate loops, it is up to your preference.
TryParse
method is not used in these tasks because it is more useful to practice the try-catch
statements as they are relevant for the upcoming topics.
try-catch
check into an infinite while loop and break the loop after the input is successfully parsed. This way you can ensure that the program re-prompts the user in case of wrong input.
using System;
public class Calculator
{
public static void Main(string[] args)
{
char opr;
float first, second;
while (true)
{
try
{
Console.Write("Enter an operation (+, -, /, *): ");
opr = Console.ReadLine()[0];
// 'Contains' method checks if a string contains a character or a substring.
if ("/-+*".Contains(opr))
break;
}
catch { }
Console.WriteLine("The operator is invalid.");
}
while (true)
{
try
{
Console.Write("Enter the first number: ");
first = float.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("The first number is invalid. ");
}
}
while (true)
{
try
{
Console.Write("Enter the second number: ");
second = float.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("The second number is invalid. ");
}
}
float result = 0;
if (opr == '+')
result = first + second;
else if (opr == '-')
result = first - second;
else if (opr == '/')
result = first / second;
else if (opr == '*')
result = first * second;
Console.WriteLine($"The result of '{first} {opr} {second}' is: {result}");
}
}
Tudo estava claro?
Conteúdo do Curso
Introduction to .NET with C#
2. Asynchronous Programming and Introduction to Web Services
3. Introduction to Databases with .NET
Introduction to .NET with C#
Task - Improving the Calculator
The code for the calculator which we created in the previous task is given below. Your task is to add checks to ensure proper user input.
- Add a check to make sure that the user enters appropriate input for the
opr
variable. The input should not be empty and should only be equal to+
,-
,/
or*
. In case of wrong input, it should prompt the user again for entry. Similar to point 1, add checks forfirst
andsecond
. Make sure the user enters a valid number.
There can be multiple ways to do this. As long as it satisfies the condition, it is a valid solution. Ideally there should be 3 separate try-catch statements for the 3 inputs. You can put all the try-catch statements in a single loop, or separate loops, it is up to your preference.
TryParse
method is not used in these tasks because it is more useful to practice the try-catch
statements as they are relevant for the upcoming topics.
try-catch
check into an infinite while loop and break the loop after the input is successfully parsed. This way you can ensure that the program re-prompts the user in case of wrong input.
using System;
public class Calculator
{
public static void Main(string[] args)
{
char opr;
float first, second;
while (true)
{
try
{
Console.Write("Enter an operation (+, -, /, *): ");
opr = Console.ReadLine()[0];
// 'Contains' method checks if a string contains a character or a substring.
if ("/-+*".Contains(opr))
break;
}
catch { }
Console.WriteLine("The operator is invalid.");
}
while (true)
{
try
{
Console.Write("Enter the first number: ");
first = float.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("The first number is invalid. ");
}
}
while (true)
{
try
{
Console.Write("Enter the second number: ");
second = float.Parse(Console.ReadLine());
break;
}
catch
{
Console.WriteLine("The second number is invalid. ");
}
}
float result = 0;
if (opr == '+')
result = first + second;
else if (opr == '-')
result = first - second;
else if (opr == '/')
result = first / second;
else if (opr == '*')
result = first * second;
Console.WriteLine($"The result of '{first} {opr} {second}' is: {result}");
}
}
Tudo estava claro?