2012-09-26 52 views
3

我已經創建了一個程序,按下計算按鈕,平均速度將使用總距離和總時間計算出來,這將是乘以從紐約市到邁阿密的時間,以獲得從紐約市到邁阿密的距離。點擊後按鈕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

+2

什麼錯誤?和哪裏? – Guillaume

回答

5

我認爲錯誤是在這個函數

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(); 
      } 
+0

+1是的,謝謝。現在我得到這個函數的錯誤:'不是所有的代碼路徑都返回一個值' – techAddict82

+1

@ charliecodex23將它的返回類型改爲'void'。 – Guillaume

+0

@freelancer謝謝你,解決了這個問題,我遇到的最後一個問題是:'private double get_Time'給tbTime一個錯誤,不會被聲明。我正在嘗試獲取textBox3的值並將其轉換爲一個int來計算其餘值。 – techAddict82