.NET 4.x Исключение Format.Exeption в приложении "калькулятор", как исправить ошибку? - C#
Формулировка задачи:
namespace ConsoleApplication3
{
class Калькулятор
{
static void Main()
{
double a;
double b;
double res;
char answer='Y';
begin: Console.WriteLine("a=");
a = double.Parse(Console.ReadLine()); Компилятор выдает необработанное исключение Format.Exeption. Причем после первого правильного расчета
Console.WriteLine("знак операции");
char op = (char)Console.Read();
Console.ReadLine();
Console.WriteLine("b=");
b = double.Parse(Console.ReadLine());
bool ok = true;
switch (op)
{
case '+': res = a + b; break;
case '-': res = a - b; break;
case '*': res = a * b; break;
case '/': res = a / b; break;
default: res = double.NaN;
ok = false; break;
}
if (ok) Console.WriteLine("Результат=" + res);
else Console.WriteLine("Недопустимая операция");
Console.WriteLine("Закончить? Y/N");
answer = (char)Console.Read();
if (answer != 'Y') goto begin;
else Console.WriteLine("Закончили");
}
}
}Решение задачи: «.NET 4.x Исключение Format.Exeption в приложении "калькулятор", как исправить ошибку?»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Калькулятор
{
static void Main()
{
double a, b, res;
char answer;
begin:
Console.WriteLine();
Console.WriteLine("a=");
a = double.Parse(Console.ReadLine());
Console.WriteLine("знак операции:");
char op = Console.ReadKey().KeyChar;
Console.WriteLine();
Console.WriteLine("b=");
b = double.Parse(Console.ReadLine());
bool ok = true;
switch (op)
{
case '+': res = a + b; break;
case '-': res = a - b; break;
case '*': res = a * b; break;
case '/': res = a / b; break;
default: res = double.NaN;
ok = false; break;
}
if (ok) Console.WriteLine("Результат=" + res);
else Console.WriteLine("Недопустимая операция!");
Console.WriteLine("Закончить? Y/N");
answer = Console.ReadKey().KeyChar;
if (answer != 'Y')
goto begin;
else Console.WriteLine("Закончили");
}
}
}