No, that won't work.
Look at your code, and it's obvious that its isn't right: which one of the switch statements is going to be executed? None of them - because
k
is always an empty string.
For starters, don't use
float.parse
to convert numbers: use
float.TryParse
or better, use
double.TryParse
instead. And what I'd suggest is writing a method to return a number:
private static double GetNumber(string prompt)
{
double result;
do
{
Console.WriteLine(prompt);
string inp = Console.ReadLine();
if (double.TryParse(inp, out result))
{
break;
}
Console.WriteLine("I'm sorry, but that wasn't a valid number. Please try again.");
} while (true);
return result;
}
You can then call that twice to fetch the two operands.
Then use Console.ReadLine to fetch the operator, and execute the switch statement on the user input.