【发布时间】:2021-02-28 09:29:48
【问题描述】:
我是初学者,刚开始用C#编写一个简单的计算器程序,当我在终端中运行我的应用程序时,按回车后它的用户输入仍然有ReadLine,如果我按两次回车,这些代码已发出
Unhandled exception. System.FormatException: Input string was not in a correct format.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
at System.Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
at System.Convert.ToInt32(String value)
这是我的代码
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Yor First Number");
Console.ReadLine();
int x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Yor Second Number");
Console.ReadLine();
int y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Choose your calculation");
Console.WriteLine("Type + for Addition");
Console.WriteLine("Type - for Subtraction");
Console.WriteLine("Type * for Multiplication");
Console.WriteLine("Type / for Division");
Console.ReadLine();
string expression = Console.ReadLine();
switch(expression)
{
case "+":
Console.WriteLine(x+y);
break;
case "-":
Console.WriteLine(x-y);
break;
case "*":
Console.WriteLine(x*y);
break;
case "/":
Console.WriteLine(x/y);
break;
}
}
}
}
【问题讨论】:
标签: c# switch-statement user-input calculator