2016-07-23 74 views
-4
//Add comments here that explain the Sqrt method 
     private void btnSqrt_Click(object sender, EventArgs e) 
     { 
      double num = double.Parse(textDisplay.Text); 
      if (num >= 0) 
      { 
       textDisplay.Text = SquareRoot(num).ToString(); 
      } 
      else 
      { 
       MessageBox.Show("Number must be positive", "Error Message"); 
       textDisplay.Text = "0"; 
      } 
     } 

     //Add comments here that explain the Sqrt function 
     //What are the arguments and the return value(s) 

     //To Do – Add the math sqrt method. 

     private double SquareRoot(double x) 
     { 



      textDisplay.Text = Convert.ToString(Math.Sqrt(Convert.ToDouble(x))); 


     } 

我遇到了數學sqrt方法的麻煩。 對於這個問題我一直在給第一行
私人雙SquareRoot(雙x) 我試圖寫的方法,但我有一個紅線在SquareRoot下。 我的方法怎麼了? 感謝 它的一個計算器數學sqrt函數。我在想什麼

+2

你應該學會看幫助VS來這裏之前提供近了!它肯定告訴你,你的方法缺少你在簽名中承諾的返回值。 – TaW

回答

0

你必須提供一個return語句是這樣的:

private double SquareRoot(double x) 
{ 
    return Math.Sqrt(x); 
} 

...或使其void ...這樣的:

private void SquareRoot(double x) 
+0

您正在調用'Math.Sqrt'和'Convert.ToDouble'兩次。 'Convert.ToDouble'甚至不需要,因爲參數已經是'double'類型了。 – Wazner

+0

@Wazner,是的,我正在編輯它。你可以不投票給我嗎? –

+0

沒問題,我刪除了我的downvote。在快速編輯:) – Wazner

1

你的方法規定它會返回一個double值,但其正文中沒有返回語句。爲了糾正這個錯誤,你的方法更改爲以下:

private double SquareRoot(double x) 
{ 
    return Math.Sqrt(x); 
} 

我刪除了Convert.ToDouble看到,因爲你的參數的類型爲double,也去掉了Convert.ToString,因爲你已經調用ToString早些時候在函數被調用。

+0

感謝您的明確解釋。 – Deise

0

您的方法應爲return a double,並且不應更改textbox屬性。

private double SquareRoot(double x) 
{ 
    return Math.Sqrt(x); 
} 
0

你必須返回值

private double SquareRoot(double x) 
{ 
    return Math.Sqrt(x); 
}