我正在编写一个计算势能变化的复杂程序(ΔPE= mgΔh)。
Console.WriteLine("Mass in kg");
string cmMa = Console.ReadLine();
double cMass;
if(!Double.TryParse(cmMa, out cMass) )
{
Console.WriteLine("Only numbers!");
Console.ReadLine();
}
Console.WriteLine("First height in m");
string cfH = Console.ReadLine();
double fH;
if(!Double.TryParse(cfH, out fH))
{
Console.WriteLine("Only numbers!");
Console.ReadLine();
}
Console.WriteLine("Second height in m");
string csH = Console.ReadLine();
double sH;
if(!Double.TryParse(csH, out sH))
{
Console.WriteLine("Only numbers!");
Console.ReadLine();
}
double ch = fH - sH;
Console.WriteLine("Intermediate result: Change in height(Δh)= "+ch+" m" );
Console.ReadLine();
double ng = 9.81; // m/s^2
Console.WriteLine("CHANGE IN POTENTIAL ENERGY: "+ch*cMass*ng+" J");
最后,我想添加一个 if 语句,如果 sH>fH,则交换它们的值。我应该应用什么概念?