c# - 函数 : Calculating velocity, 时间、距离(double型变量)

标签 c#

我创建了一个程序,当按下计算按钮时,将使用总行驶距离和总行驶小时数计算平均速度,然后乘以从纽约市到迈阿密的时间,得到从纽约市到迈阿密的距离。单击按钮后 Label1 将报告速度、时间和距离,Clear_textBox() 将清除所有文本框字段。问题是我没有得到任何结果,而是错误。

有人可以指导我对代码进行更改吗?

我收到错误的函数 Display_Results 和属性 private double get_Time

   namespace form1
   {
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }



    private double Calculate_Velocity()
    {
        //Calculate Velocity
        int startingMileageBox;
        startingMileageBox = Convert.ToInt32(textBox1.Text);

        int endingMileageBox;
        endingMileageBox = Convert.ToInt32(textBox2.Text);

        double time = Get_Time(); //assuming you have set up GetTime()
        double distance = endingMileageBox - startingMileageBox;
        return distance / time; 
    }

   public double Get_Time()
    {
        //Get Time
        return get_Time;
    }

    private double Calculate_Distance(double velocity, double time)
    {
        //Calculate Distance
        return velocity * time;
    }

    private void Display_Results(double velocity, double time, double distance)
    {
        //Display Results
        label1.Text = "Velocity = " + velocity.ToString() + " time= " + time.ToString() + " Distance = " + distance.ToString();
    }

    private void Clear_Textboxes()
    {
        //Clear textboxes
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
    }



    // Property to GetTime
    private double get_Time
    {


        get
        {



            // variable to hold time
            double time = double.MinValue;

            // Safely parse the text into a double
            if (double.TryParse(textBox3.Text, out time))
            {
                return time;
            }

            // Could just as easily return time here   
            return double.MinValue;
        }
        set
        {
            // Set tbTime
            textBox3.Text = value.ToString();
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        Close(); 
    }

    private void button1_Click(object sender, EventArgs e)
    {
        double v = Calculate_Velocity();
        double t = Get_Time();

        double d = Calculate_Distance(v, t);
        Display_Results(v, t, d);

        Clear_Textboxes();

    }
}

表单

enter image description here

最佳答案

我认为这个函数有错误

private double Display_Results(double velocity, double time, double distance)
            {
                //Display Results
                label1.Text = time + velocity + distance;
            }

请将时间+速度+距离值转换为字符串格式。

尝试跟随

private double Display_Results(double velocity, double time, double distance)
            {
                //Display Results
                double v=-velocity;
                double t=-time;
                double d=-distance;

                label1.Text = (t + v + d).ToString();
            }

关于c# - 函数 : Calculating velocity, 时间、距离(double型变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12594104/

相关文章:

c# - 为什么 Event 在 C# 中多次触发?

c# - 具有 IEnumerable<ISomeInterface> 类型属性的 NewtonSoft.Json 序列化和反序列化类

c# - Delphi Pipes.PAS 与 .NET 命名管道不兼容?

c# - 要求表单例份验证

c# - 将 Composition 用于“is – a”关系的问题

c# - ASP.NET CORE 版本中的 HttpContext.Current.Request.Form.AllKeys

c# - 如何调试由 Windows 服务托管的 WCF

c# - 在本地计算机中使用 Azure 移动服务进行开发[.Net 后端]

c# - 具有 IoT 中心的 Azure Functions 无法检索分区

C# 3.0,对象初始化器和获取属性返回数组,如何初始化?