如何将变量从Main发送到另一个方法,然后将值返回给Main方法?
我想将变量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
您应该将ComputeWindChill
标记为静态。否则,你不能使用它。此外,您必须在某处存储您调用的方法的结果,然后使用它。由于你的代码是,即使它会与我们的错误一起编译,你调用了这个方法,但你并没有使用它的结果。最后但并非最不重要的方法需要3个参数,你只通过1
并且它需要3个参数而不是1个参数。并没有使用返回值。和.. –
@SergeyBerezovskiy我后来注意到了,我添加了它。谢谢 ! – Christos
你必须改变你的方法是:
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
回报paramenter,并在第三个参数out
关键字。
这个方法的调用是有点不同:
double windChill;
ComputeWindChill(temperature, windSpeed,out windChill);
Console.Write("Temperature (including windchill): {0}", windChill);
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.我正在打电话
您需要先了解如何定义一个函数。 例如采取您的功能。 在您的函数中,温度和风速是您希望发送给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);
您可以使用“走出去”调用方法
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;
}
}
什么是你的代码错误后,又回到Windchill的价值?错误?意外的结果? –
'ComputeWindChill'会返回一个值,但不会捕获它。它也需要3个参数(但似乎应该只需要2个,因为一个是结果)代码只能通过1 – Plutonix