C# 将变量传递给方法

C# 将变量传递给方法,c#,methods,parameter-passing,C#,Methods,Parameter Passing,嘿,我试图将一个全局变量传递给一个方法,并在while循环中多次调用它。我不知道这是怎么回事,我不知道这是怎么回事。它看起来像是我看过的一些例子中应该有的,但显然不是。我想做的是让体面的变量增加到250,我在while循环外测试了这个函数[它在那里对我有效,但在它里面,它不是那么多,它只是保持0,而高度每次通过while循环都在下降。谢谢你在这方面的帮助 using System; using System.Collections.Generic; using System.Linq; usin

嘿,我试图将一个全局变量传递给一个方法,并在while循环中多次调用它。我不知道这是怎么回事,我不知道这是怎么回事。它看起来像是我看过的一些例子中应该有的,但显然不是。我想做的是让体面的变量增加到250,我在while循环外测试了这个函数[它在那里对我有效,但在它里面,它不是那么多,它只是保持0,而高度每次通过while循环都在下降。谢谢你在这方面的帮助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// Every measuerments are in
//Feet
//Seconds
//This program is to sim a simple jump with no graphics. Work the numbers out for final             implementaion .
namespace Jumper1Test
{
   class Program
   {
    //10 - 20 feet above ground pull both cords to slow down to about 0 ft per second
    private static int alt;           //in feet 30,000 20,000, 15,000 ft *Note keep decent same scale\measurment
    private static float decent = 0;     //speed of jumper 250ft per second cute deploed 15-20ft p[er second want to hit 0 when landing
    private static int cuteDelay = 3; //3 second cute delay after opeing (ruf estimate on average)
    private static bool leftCord;
    private static bool rightCord;
    private static bool cuteDeployed; //if parachute is deployed
    private static bool jumped;       //jump iniciated


    //environtmnet effects
    private  enum windDrection {North, South, East, West, NE, NW, SE, SW } //NE NW = North East West respectivly
    private static int windspeed; //in knots

    static void Main(string[] args)
    {
        Console.WriteLine("Jump Sim 1.0");

        //select the hight for the jump
        Console.WriteLine("Enter Jump Altitued:");
        Console.WriteLine("a for 30000 Ft");
        Console.WriteLine("b for 25000 Ft");
        Console.WriteLine("c for 15000 Ft");
        String alt1 = Console.ReadLine();
        if (alt1.Equals("a"))
        {
            alt = 30000;
        }
        else if (alt1.Equals("b"))
        {
            alt = 25000;
        }else { alt = 15000; }
        Console.WriteLine("The Hight of the jump is " + alt);


        //jumping
        int countdown = 5;
        while (countdown != 0)
        {
            Console.WriteLine("Jumping in " + countdown);
            System.Threading.Thread.Sleep(1000); //wait for 1 secod.
            countdown--;

        }
        Console.WriteLine("Jump!");
        while (alt != 0)
        {
            alt = alt - 5000;
            Console.WriteLine("Altitue = " + alt);
            velocity(decent);
            Console.WriteLine("Speed is: " + decent);
        }

        // keep screen from going away
        // when run from VS.NET
        Console.ReadLine();
    }



    private static float velocity(float decent)
    {

            for (int i = 0; i < 8; i++) //its takes 8 seconds to reach terminal velocity
            {
                decent = decent + 31.25f; //increease speed of fall
                System.Threading.Thread.Sleep(1000); //wait for 1 secod.



            }
            return decent;


    }//end of velocity


}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
//每一项措施都很有效
//脚
//秒
//这个程序是模拟一个没有图形的简单跳转。计算出最终实现的数字。
命名空间跳线1测试
{
班级计划
{
//离地10-20英尺拉两根绳索,使其速度降至每秒0英尺左右
私有静态int alt;//单位:30000英尺20000,15000英尺*注意保持相同的比例\measurement
私人静态浮动下降=0;//跳线速度每秒250英尺可爱的下降15-20英尺p[er秒着陆时想达到0
私有静态int cuteDelay=3;//操作后3秒cute延迟(ruf平均估计)
专用静电线;
专用静电门;
私有静态布尔已断开部署;//如果已部署降落伞
私有静态bool跳转;//跳转已初始化
//环境净效应
私有枚举风分区{北、南、东、西、东北、西北、东南、西南}//NE NW=东北-西
私有静态int风速;//以节为单位
静态void Main(字符串[]参数)
{
Console.WriteLine(“跳转Sim 1.0”);
//选择跳转的高度
Console.WriteLine(“输入跳转高度:”);
控制台写入线(“30000英尺的a”);
控制台。写线(“b代表25000英尺”);
控制台写入线(“c为15000英尺”);
字符串alt1=Console.ReadLine();
如果(备选1.等于(“a”))
{
alt=30000;
}
否则,如果(备选1等于(“b”))
{
alt=25000;
}else{alt=15000;}
Console.WriteLine(“跳跃的高度为”+alt);
//跳跃
整数倒计时=5;
while(倒计时!=0)
{
控制台。写线(“跳入”+倒计时);
System.Threading.Thread.Sleep(1000);//等待1秒。
倒计时--;
}
控制台。写线(“跳转!”);
while(alt!=0)
{
alt=alt-5000;
Console.WriteLine(“Altitue=“+alt”);
速度(下降);
控制台.WriteLine(“速度为:“+delegate”);
}
//不要让屏幕消失
//从VS.NET运行时
Console.ReadLine();
}
专用静态浮动速度(浮动下降)
{
对于(inti=0;i<8;i++)//达到终点速度需要8秒
{
下降=下降+31.25f;//增加下降速度
System.Threading.Thread.Sleep(1000);//等待1秒。
}
回归正派;
}//速度终点
}
}
我想您需要:

 //velocity(decent);
 decent = velocity(decent);
这也意味着下降不一定是一个全局变量,它可以成为Main()的一个适当的局部变量。 作为编写更好的软件的第一步,一定要尽量避免使用全局语言

也尝试

//Console.WriteLine("Jumping in " + countdown);
Console.Write("Jumping in {0} \r", countdown);
对于一些令人眼花缭乱的视频效果。

我想您需要:

 //velocity(decent);
 decent = velocity(decent);
这也意味着下降不一定是一个全局变量,它可以成为Main()的一个适当的局部变量。 作为编写更好的软件的第一步,一定要尽量避免使用全局语言

也尝试

//Console.WriteLine("Jumping in " + countdown);
Console.Write("Jumping in {0} \r", countdown);

对于一些令人眼花缭乱的视频效果。

浮点值是按值传递的。您需要将返回值重新指定为:

decent = velocity(decent);

另一个选项是,因为它是一个成员变量,所以不要将其传递给方法。在该方法中,您指的是传入的变量,而不是您的成员变量。

浮点数是按值传递的。您需要将返回值赋回:

decent = velocity(decent);

另一个选项是,因为它是一个成员变量,所以不要将其传递给方法。在该方法中,您指的是传入的变量,而不是您的成员变量。

我认为您看到的问题是,您希望在
velocity
中设置类的fiel值d定义为
public static float delegate;
。这个问题是因为您的参数也被命名为
delegate
,在
velocity
的上下文中,它覆盖了范围更广的
delegate
的含义。为了达到这个效果,您可以将
delegate
参数重命名为somet其他或使用:

this.decent = decent + 31.25f
我不太明白为什么
delegate
是一个字段,但如果您希望将其传递给成员函数。这可以通过将
velocity
的声明更改为:

private static void velocity()

然后保持当前的使用状态。

我认为您看到的问题是,您希望在
velocity
中使用
Delegate=Delegate+31.25f;
来设置类字段的值,该字段定义为
公共静态浮点Delegate;
。这是因为您的参数也被命名为
Delegate
>在
velocity
的上下文中,它会覆盖范围更广的
Delegate
的含义。为了实现此效果,您可以将
Delegate
参数重命名为其他参数,或者使用:

this.decent = decent + 31.25f
我不太明白为什么
delegate
是一个字段,但如果您希望将其传递给成员函数。这可以通过将
velocity
的声明更改为:

private static void velocity()
然后把你的用途留给c