2014-10-27 36 views
-2

我想初始化3種不同的方法並獲取錯誤。爲什麼我得到這個錯誤試圖創建這些方法?

這個變量不會在目前情況下

我想它,因爲範圍存在,但我不知道怎樣才能改變calculatebmi()calculateextime()方法不搞亂起來。

我該如何擺脫這個錯誤?它抱怨oldbmi,timenewbmi

相關代碼:

public partial class Program2 : Form 
{ 
    private double _height; 
    private double _weight; 
    const int caloriesBurned = 10000; 
    const int walkingSpeed = 4; 
    const double fatCaloriesPerPound = 3500; 
    const double metricWalkingSpeed = walkingSpeed/.62137; 

    private double calculateBmi(double metricWeight, double metricHeight) 
    { 
     double oldBmi = metricWeight/Math.Pow(metricHeight, 2); 


     double newMetricWeight = metricWeight - (caloriesBurned/(fatCaloriesPerPound * 2.2046)); 
     double newBmi = newMetricWeight/Math.Pow(metricHeight, 2); 

     return oldBmi; 
    } 

    private double calculateExTime(double metricWeight, double metricHeight) 
    { 
     double exerciseMultiplier = .0215 * Math.Pow(metricWalkingSpeed, 3) 
            - .1765 * Math.Pow(metricWalkingSpeed, 2) 
            + .8710 * metricWalkingSpeed 
            + 1.4577; 
     double time = caloriesBurned/(exerciseMultiplier * metricWeight); 

     return time; 
    } 


     private void displayresults(double _height, double _weight, double oldbmi,double time, double newBmi) 
     { 
      double newWeight = _weight - (caloriesBurned/fatCaloriesPerPound); 
      int feet = (int)_height/12; 
      int inches = (int)_height % 12; 
      HeightText.Text = string.Format("{0}ft {1}in", feet, inches); 
      Weight.Text = _weight.ToString(); 
      OriginalBmi.Text = oldBmi.ToString("F2"); 
      NewBmi.Text = newBmi.ToString("F2"); 
      NewWeight.Text = newWeight.ToString("F2"); 
      ExerciseTime.Text = string.Format("{0} hrs {1} min", (int)(time), (int)(time % 60)); 
     } 




displayresults(_height, _weight,oldBmi,time,newBmi); 
+3

*哪個*變量是抱怨,哪裏*?您目前已發佈超過120行代碼 - 請將其縮減爲一個簡短但完整的程序,以展示問題。 – 2014-10-27 17:49:29

+0

現在你已經刪除了*這麼多*,沒有跡象表明你在班上有什麼變數。我要求一個簡短的*但完整的*程序。 *您是否在聲明編譯器抱怨的變量?如果是這樣,在哪裏? – 2014-10-27 17:55:38

+0

那麼你在哪裏調用'displayresults'方法? – Shaharyar 2014-10-27 17:59:46

回答

1

問題似乎在您對顯示結果方法的調用中。您引用的是oldbmi,newbmi和time,但這些對象中沒有一個存在於該範圍內,因爲它們在本地定義爲calculateBmi。這是我的一個假設 - 您沒有真正展示過您調用calculateBmi的代碼的上下文 - 但似乎是這種情況。

如果要在displayresults()中計算的oldBmi和newBmi值在程序中的其他位置可用,則需要在類級別創建私有字段或公共屬性,就像您對_height和_weight所做的那樣。然後,您可以將this.newbmi設置爲您計算的值,而不是在該範圍內創建一個永遠不會使用的新double。

所以取而代之,你的類的頂部是這樣的:

private double _height; 
private double _weight; 
private double oldbmi = 0; 
private double newbmi = 0; 
const int caloriesBurned = 10000; 
const int walkingSpeed = 4; 
const double fatCaloriesPerPound = 3500; 
const double metricWalkingSpeed = walkingSpeed/.62137; 

和你calculateBmi方法看起來像這樣:

private double calculateBmi(double metricWeight, double metricHeight) 
{ 
    this.oldBmi = metricWeight/Math.Pow(metricHeight, 2); 


    double newMetricWeight = metricWeight - (caloriesBurned/(fatCaloriesPerPound * 2.2046)); 
    this.newBmi = newMetricWeight/Math.Pow(metricHeight, 2); 

    return oldBmi; 
} 

,並且可以調用與this.oldbmi和displayresults this.newbmi。

您應該知道,代碼中的幾乎所有內容都違背了.NET命名約定。變量不應以'_'開頭,consts應該是CamelCase,私有字段應該是pascalCase,方法應該始終是CamelCase。請看看這個:http://msdn.microsoft.com/en-us/library/vstudio/618ayhy6%28v=vs.100%29.aspx。這裏有很多人遵循這些慣例,這會讓你更容易在將來獲得幫助。

0

要調用的方法displayresults這需要一些參數。

無論何時調用方法,都需要將值a/c傳遞給方法參數的DataType。沒有必要像方法一樣傳遞具有確切名稱的變量(我認爲你對此有困惑)。

所以正確的語法將是這樣的:

displayresults(10, 20, 30, 40, 50); 

OR

如果你想傳遞變量,你應該讓他們在目前情況下:

double _height = 10; 
double _weight = 20; 
double oldBmi = 30; 
double time = 40; 
double newBmi = 50; 

displayresults(_height, _weight, oldBmi, time, newBmi); 
相關問題