将变量传递给方法

时间:2012-02-15 23:35:56

标签: c# methods parameter-passing

嘿我试图将一个全局变量传递给一个方法,并在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


}
}

3 个答案:

答案 0 :(得分:2)

我想你想要:

 //velocity(decent);
 decent = velocity(decent);

这也意味着decent(下降)不一定是全局变量。它可能成为Main()的正确本地。 尽量避免使用全局变量作为编写更好软件的第一步。

也可以尝试

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

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

答案 1 :(得分:1)

我认为您看到的问题是,您希望decent = decent + 31.25f;中的velocity设置定义为public static float decent;的班级字段的值。这个问题是因为您的参数名为decent,它会在decent的上下文中覆盖更广泛的范围velocity的含义。为了实现此效果,您可以将decent参数重命名为其他参数或使用:

this.decent = decent + 31.25f

我不太明白为什么decent是一个字段,但是如果你希望将它传递给成员函数。通过将velocity的声明更改为:

,可以轻松实现这一目标
private static void velocity()

然后按照您现有的方式使用它。

答案 2 :(得分:0)

浮点值按值传递。您需要将返回值分配回体面:

decent = velocity(decent);

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