我的程序有点问题.基本上,我必须创建一个程序来查找窗口的区域(例如玻璃窗口).公式是Area = Height*Width,这就是我输入的内容.但是,结果实际上并没有回答高度*宽度.如果我输入两个相同的数字(例如,3*3),答案将是正确的(9).另一方面,如果我输入两个不同的数字(例如,4*5),答案是不正确的(它会说上一个例子的答案是25,它应该是20).任何人都可以帮助我理解为什么这样做并帮助我解决问题?
PS我刚刚开始使用Microsoft Visual C#2010 Express在学校进行计算.这就是为什么它相当简单.
using System;
namespace FindTheArea
{
class Program
{
static void Main(string[] args)
{
string temporary;
double Height;
double Width;
double Area;
Console.WriteLine("Find The Area");
Console.WriteLine("Please enter the height below");
temporary = Console.ReadLine();
Console.WriteLine("Please enter the Width below");
temporary = Console.ReadLine();
Console.Clear();
Height = double.Parse(temporary);
Width = double.Parse(temporary);
Area = (Height * Width);
Console.WriteLine("The area is...");
Console.WriteLine();
Console.WriteLine(Area+"cm2");
Console.Read();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我现在明白我哪里出错了.但是我该如何解决呢?
Height = double.Parse(temporary);
Width = double.Parse(temporary);
Run Code Online (Sandbox Code Playgroud)
这里有一个明显的问题.行为double.Parse()是确定性的,因此您要为Height和分配相同的值Width.