我是C#的初学者,我想创建一个简单的计算器。我已经写了所有的代码,它没有显示任何错误,然而,它没有显示它正确。这是我正在使用的全部代码:
using System;
namespace C_
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Calculator";
float num1;
float num2;
float resultSum;
float resultSub;
float resultProd;
float resultDiv;
Console.Write("Enter your first number ");
num1 = Convert.ToInt32(Console.Read());
num2 = Convert.ToInt32(Console.Read());
resultSum = num1 + num2;
Console.Write("The sum is " + resultSum);
resultSub = num1 - num2;
Console.Write("The differnce is " + resultSub);
resultProd = num1 * num2;
Console.Write("The product is " + resultProd);
resultDiv = num1 / num2;
Console.Write("The quotient is " + resultDiv);
Console.ReadKey();
}
}
}
当我在不调试的情况下运行该程序时,控制台显示如下:
https://docs.microsoft.com/en-us/dotnet/api/system.console.writeline?view=netcore-3.1
如果将console.write...
替换为console.write...
,它将在打印语句的末尾添加换行符,因此输出应如下所示:
The sum is 63
The difference is 37
...
您的代码使用consolekey值,而不是您输入的数值。2的控制台键是50。返回的控制台键是13。