如何将变量从Main发送到另一个方法,然后将值返回给Main方法?

时间:2017-03-07 16:02:31

标签: c# console-application return-value

我想将变量temperature和windSpeed发送到ComputeWindChill方法以查找windchill。然后将windchill返回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

5 个答案:

答案 0 :(得分:1)

您可以使用" out" 在调用方法后获取windChill值

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; 
}
}

答案 1 :(得分:0)

您应将ComputeWindChill标记为静态。否则你不能使用它。此外,您必须在某处存储您调用的方法的结果,然后使用它。正如你的代码一样,即使它与错误相符,你也可以调用方法,而不是使用它的结果。最后但并非最不重要的是,该方法需要3个参数,并且只传递1.

答案 2 :(得分: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个参数(温度和风速),并在所有计算中返回一个双精度。您还应该注意到该方法必须声明为static,因为您是从另一个静态方法(main

调用它

电话应该是这样的:

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返回参数,以及第三个参数中的out关键字。

对此方法的调用有点不同:

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

答案 3 :(得分: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。我在手机上打字

答案 4 :(得分:0)

您首先需要了解如何定义函数。 例如,你的功能。 在您的函数中,温度和windspeed是您希望发送到ComputeWindChill函数的参数。并且您需要从函数返回双值windspeed。

所以你将你的函数定义为

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);