(请记住,我是编码新手,所以我提前为任何明显的错误道歉)
我目前正在尝试使用用户通过控制台提供的大炮的速度和角度来计算弹道炮的飞行时间(以及最终的距离)。然而,数学结果是错误的。评论中的 Tof 计算来自旧行,我只是将其用作参考,直到一切正常并且我清理它。由于当前的 Tof 正是等式所要求的:
t = (v*sin(θ) + (v2sin2(θ) + 20*L*sin(θ))1/2 )/10其中 v 是炮弹的速度,θ 是与地面的角度,L是炮管的长度。)
(除了 sin^2(theta) 到其 Trig Identity 的交换),我认为问题在于从度数到弧度的转换。当前的行是我假设的,因为我没有被教过任何关于此的内容。关于转换的任何在线结果都给了我一些类似的东西:
private double DegreeToRadian(double angle)
{
return Math.PI * angle / 180.0;
}
当它被放在当前行的位置时,它会导致它下面的几乎所有东西出现大量错误。我相信这个块本身就是代码的一部分,而不是被塞进所有东西的中间,但我不知道有什么其他方法可以做到这一点,因为所有工作都必须在 main 方法中完成。
很感谢任何形式的帮助。完整的代码如下:
Edit1:这是输入上述代码的时候
Edit2:进行了以下更改:
- @pilotcam 建议将所有内容更改为双打
- @Shannon Holsinger 建议将 int.parse 更改为 double.parse
@Shannon Holsinger - 第一个错误在结尾
double EnteredVelocityNum = double.Parse(EnteredVelocity);
紧跟在分号之后。它显示为“ } 预期”。
@MethodMan - 非常抱歉,但我不明白您的建议:
double EnteredAngleNum = 0d;
我把它放在哪里?我再次为我的无知道歉,我以前没有学过这个,这对我来说都是新的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW1
{
class Cannonball
{
static void Main(string[] args)
{
const double Length = 2;
//here the velocity of the cannonball is acquired from the player
string EnteredVelocity;
Console.WriteLine("Enter the velocity of the cannonball betweeen 0-100 meters: ");
EnteredVelocity = Console.ReadLine();
Console.WriteLine("You entered: " + EnteredVelocity);
// the angle of the cannon is acquired from the player
string EnteredAngle;
Console.WriteLine("Enter the angle of the cannon betweeen 0-90 degrees: ");
EnteredAngle = Console.ReadLine();
Console.WriteLine("You entered: " + EnteredAngle);
//calculating the Time of Flight
double EnteredAngleNum = double.Parse(EnteredAngle);
double EnteredVelocityNum = double.Parse(EnteredVelocity);
private double EnteredAngleRad(double EnteredAngleNum)
{
return (Math.PI / 180) * EnteredAngleNum;
}
//double EnteredAngleRad = (2 * Math.PI / 360) * EnteredAngleNum;
//double ToF = Math.Pow(EnteredVelocityNum * Math.Sin(EnteredAngleRad) + (Math.Pow(EnteredVelocityNum,2)) * (Math.Pow(Math.Sin(EnteredAngleRad),2)) + 20 * Length * (Math.Sin(EnteredAngleRad)) , 0.5 ) / 10; //This is trying to use Sin^2(theta) and gives 3.---
double ToF = (EnteredVelocityNum * Math.Sin(EnteredAngleRad) + (Math.Pow((Math.Pow(EnteredVelocityNum, 2.0) * ((1.0 / 2.0) - ((1.0 / 2.0) * Math.Cos(2 * EnteredAngleRad))) + 20.0 * Length * (Math.Sin(EnteredAngleRad))), 0.5))) / 10.0; //This is trying to use Trig Identities and gives 4.06----
Console.WriteLine("The Flight Time is: " + ToF);
}
}
}