I am a beginner programmer, I decided to make this calculator program to test my knowledge on basic C# syntax, the code executes as intended, however I would like to shorten it to be more concise.
Console.WriteLine("Please select your first number: ");
double numberOne = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please select your second number: ");
double numberTwo = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please choose the operation: ");
var userInput = Console.ReadLine();
if (userInput == "+")
{
Console.WriteLine(numberOne + numberTwo);
}
else if (userInput == "-")
{
Console.WriteLine(numberOne - numberTwo);
}
else if (userInput == "*")
{
Console.WriteLine(numberOne * numberTwo);
}
else if (userInput == "/")
{
Console.WriteLine(numberOne / numberTwo);
}
1 Answer 1
Your code is good, but there are a few improvements that you could make.
Determining what operation your user wants.
Instead of using if/else
, I'd probably use a switch/case
statement:
switch userInput
{
case "+":
Console.WriteLine(numberOne + numberTwo);
break;
case "-":
Console.WriteLine(numberOne - numberTwo);
break;
case "*":
Console.WriteLine(numberOne * numberTwo);
break;
case "/":
Console.WriteLine(numberOne / numberTwo);
break;
}
Error handling with user inputs
I'd probably also put a default
clause, just in case your user doesn't enter one of + - * /
:
switch userInput
{
...
default:
Console.WriteLine("Sorry, unknown operation.");
break;
}
I would also use a try/catch
block for figuring out what numberOne
and numberTwo
are:
try
{
Console.WriteLine("Please select your first number: ");
double numberOne = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please select your second number: ");
double numberTwo = Convert.ToDouble(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Sorry, invalid number.");
}
You might also want to check whether numberTwo
is not zero when dividing:
switch userInput
{
...
case "/":
if numberTwo == 0
{
Console.WriteLine("Sorry, cannot divide by zero.");
}
else
{
Console.WriteLine(numberOne / numberTwo);
}
break;
}
-
5\$\begingroup\$ And while you're at it (it being "error handling") you may want to check that
numberTwo
is not zero when dividing. \$\endgroup\$CompuChip– CompuChip2023年04月24日 14:03:31 +00:00Commented Apr 24, 2023 at 14:03 -
11\$\begingroup\$ As an alternative for the
try/catch
, you could also usedouble.TryParse
. That would also simplify making the app loop until valid numbers are entered. \$\endgroup\$minnmass– minnmass2023年04月24日 14:42:51 +00:00Commented Apr 24, 2023 at 14:42 -
1\$\begingroup\$ @CompuChip, is this what you mean (I have added a section). \$\endgroup\$sbottingota– sbottingota2023年04月25日 05:15:45 +00:00Commented Apr 25, 2023 at 5:15
-
\$\begingroup\$ Don't use try catch for converting numbers. Use
double.TryParse
instead. Try catch will have an impact on performance in duration each time the users enters an invalid number. \$\endgroup\$H. Pauwelyn– H. Pauwelyn2023年04月25日 10:13:25 +00:00Commented Apr 25, 2023 at 10:13 -
\$\begingroup\$ Also, there's no need to duplicate operations (
WriteLine
). Logic should only modify the data. So create aresult
variable. Modify it as many times as you need. Print it once. \$\endgroup\$akinuri– akinuri2023年04月25日 12:41:25 +00:00Commented Apr 25, 2023 at 12:41
Console.WriteLine()
, and the last line in the file is line 28, the closing brace of the finalelse if
block. If OP is just starting with C#, it's likely that their file only contains what's shown in the question (and perhaps are not aware of namespaces,public static void Main
etc.) \$\endgroup\$