2017-03-07 288 views
0

我想將變量temperature和windSpeed發送到ComputeWindChill方法來查找windchill。然後將windchill返回到Main方法以顯示溫度,風速和包括風寒的溫度。如何將變量從Main發送到另一個方法,然後將值返回給Main方法?

public class Program 
{ 

    public static void Main(string[] args) 
    { 

     Random rnd = new Random(); 

     int temperature = rnd.Next(0,50); 
     int windSpeed = rnd.Next(4,30); 

     Console.Write("Temperature: {0}", temperature); 
     Console.WriteLine(); 
     Console.Write("Wind Speed: {0}", windSpeed); 
     Console.WriteLine(); 
     Console.Write("Temperature (including windchill): {0}", ComputeWindChill.windChill); 

     ComputeWindChill(temperature); 
     ComputeWindChill(windSpeed); 
     } // end Main 

     public double ComputeWindChill(int temperature, int windSpeed, double windChill) 
     { 
       windChill = 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) + 
        0.4275 * temperature * Math.Pow(windSpeed,0.16); 

     return windChill; 
    } 
} // end class 
+0

什麼是你的代碼錯誤後,又回到Windchill的價值?錯誤?意外的結果? –

+1

'ComputeWindChill'會返回一個值,但不會捕獲它。它也需要3個參數(但似乎應該只需要2個,因爲一個是結果)代碼只能通過1 – Plutonix

回答

0

您應該將ComputeWindChill標記爲靜態。否則,你不能使用它。此外,您必須在某處存儲您調用的方法的結果,然後使用它。由於你的代碼是,即使它會與我們的錯誤一起編譯,你調用了這個方法,但你並沒有使用它的結果。最後但並非最不重要的方法需要3個參數,你只通過1

+0

並且它需要3個參數而不是1個參數。並沒有使用返回值。和.. –

+0

@SergeyBerezovskiy我後來注意到了,我添加了它。謝謝 ! – Christos

0

你必須改變你的方法是:

public static double ComputeWindChill(int temperature, int windSpeed) 
{ 
    return 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) + 
     0.4275 * temperature * Math.Pow(windSpeed,0.16); 
} 

這將接受2個參數(溫度和風速),並會返回一個雙帶所有的計算。你也應該注意到,方法必須因爲你是從另一個靜態方法調用它被聲明爲staticmain

通話應該是這樣的:

double windChill=ComputeWindChill(temperature,windSpeed); 
Console.Write("Temperature (including windchill): {0}", windChill); 

另一種方法是使用out參數。在這種情況下,而不是方法「迴歸」的值,它的作用是設定爲out參數傳遞變量中的值:

public void ComputeWindChill(int temperature, int windSpeed, out double windChill) 
{ 
    windChill= 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed, 0.16) + 
      0.4275 * temperature * Math.Pow(windSpeed, 0.16); 
} 

通知的void回報paramenter,並在第三個參數out關鍵字。

這個方法的調用是有點不同:

double windChill; 
ComputeWindChill(temperature, windSpeed,out windChill); 
Console.Write("Temperature (including windchill): {0}", windChill); 
0
public static void Main(string[] args) 
{ 

Random rnd = new Random(); 

int temperature = rnd.Next(0,50); 
int windSpeed = rnd.Next(4,30); 

Console.Write("Temperature: {0}", temperature); 
Console.WriteLine(); 
Console.Write("Wind Speed: {0}", windSpeed); 
Console.WriteLine(); 

//here you make a new double called windchill which will be equal to what your calculate windchill method returns 
double windChill = ComputeWindChill(temperature,windSpeed) 

Console.Write("Temperature (including windchill): {0}", windChill); 

} // end Main 

//I removed your third parameter called windchill here because you do not know it yet, you are calculating it and make this a static method(read up on static methods - very important) 
public static double ComputeWindChill(int temperature, int windSpeed) 
{ 
     double windChill = 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) + 
     0.4275 * temperature *  Math.Pow(windSpeed,0.16); 

return windChill; 
} 

P.S.我正在打電話

0

您需要先了解如何定義一個函數。 例如採取您的功能。 在您的函數中,溫度和風速是您希望發送給ComputeWindChill函數的參數。而且你需要從函數返回一個雙倍的風速。

所以你定義功能

public static double ComputeWindChill(int temperature, int windSpeed) 
{ 
    return 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) + 
    0.4275 * temperature * Math.Pow(windSpeed,0.16); 
} 

,並同時調用上面的函數,你應該傳遞參數的功能,都在同一時間和存儲從函數預期的返回值。

double windChill=ComputeWindChill(temperature,windSpeed); 
1

您可以使用「走出去」調用方法

public class Program 
{ 
public static void Main(string[] args) 
{ 
Random rnd = new Random(); 
int temperature = rnd.Next(0,50); 
int windSpeed = rnd.Next(4,30); 

Console.Write("Temperature: {0}", temperature); 
Console.WriteLine(); 
Console.Write("Wind Speed: {0}", windSpeed); 
Console.WriteLine(); 

// Not sure what you are trying to do here? 
Console.Write("Temperature (including windchill): {0}",ComputeWindChill.windChill); 

double windChill=0; 

ComputeWindChill(temperature,windSpeed,windChill); 

ComputeWindChill(windSpeed); 
} // end Main 

public static double ComputeWindChill(int temperature,int windSpeed,out double windChill) 
{ 
     windChill = 35.74 + 0.6215 * temperature - 35.75 * Math.Pow(windSpeed,0.16) + 
      0.4275 * temperature * Math.Pow(windSpeed,0.16); 

return windChill; 
} 
} 
相關問題