首页 / C# / C#使用私有双重函数来计算距离
C#使用私有双重函数来计算距离
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#使用私有双重函数来计算距离,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1750字,纯文字阅读大概需要3分钟。
内容图文
![C#使用私有双重函数来计算距离](/upload/InfoBanner/zyjiaocheng/800/b4f3572a98324b029e1d90f71e02255d.jpg)
我刚刚被介绍到C#编码世界.目前,我正在制定一个程序,该计划将使用行驶的总距离和行驶的总小时数来计算平均速度,并且该结果将乘以从纽约市到迈阿密的距离,以获得从纽约市到迈阿密的距离.我在表单上放了四个textBox,一个按钮进行计算.
我需要帮助构建功能.例如,为了计算速度:速度=距离/时间.如何在CalculateVelocity()函数中以正确的格式放置该信息?
4个TextBox及其标签(这是用户输入数据的地方):
Starting Mileage
Ending Mileage
Total Driving Time
Time from NY city to MIAMI
我正在使用的代码函数:
private double CalculateVelocity()
{
//Calculate Velocity
}
public double GetTime()
{
//Get Time
return GetTime;
}
private double CalculateDistance(double velocity, double time)
{
//Calculate Distance
}
private double DisplayResults(double velocity, double time, double distance)
{
//Display Results
}
private double ClearTextboxes()
{
//Clear textboxes
}
// Property to GetTime
private double GetTime
{
get
{
// variable to hold time
double time = double.MinValue;
// Safely parse the text into a double
if (double.TryParse(tbTime.Text, out time))
{
return time;
}
// Could just as easily return time here
return double.MinValue;
}
set
{
// Set tbTime
tbTime.Text = value.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
//Calculate and display result in a label
}
解决方法:
CalculateVelocity应如下所示:
private double CalculateVelocity()
{
double time = GetTime(); //assuming you have set up GetTime()
double distance = endingMileageBox - startingMileageBox;
return distance/time;
}
其中endingMileageBox是结束里程文本框中的值,startingMileageBox是起始里程文本框中的值.
根据你的评论,这是CalculateDistance应该是这样的:
private double CalculateDistance(double velocity, double time)
{
//note that this assumes the units match up. If not, you'll need to do some conversions here
return velocity * time;
}
}
内容总结
以上是互联网集市为您收集整理的C#使用私有双重函数来计算距离全部内容,希望文章能够帮你解决C#使用私有双重函数来计算距离所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。