2012-02-15 242 views
0

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

我没有看到任何问号。 – 2012-02-15 23:38:45

+1

你是如何“尝试将全局变量传递给方法”而全局变量*在C#中根本不存在? – Krizz 2012-02-15 23:38:49

+0

您不会将'velocity'方法的返回值分配给一个变量。您传递给方法的变量不能在方法内部更改,因为它是一个值类型。 – adrianbanks 2012-02-15 23:38:56

回答

2

我想你想要的任何帮助:

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

而这也意味着,decent(后裔)并不一定是一个全局变量。它可能成为Main()的适当本地。 尽量避免使用全局变量作为编写更好软件的第一步。

也可以尝试

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

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

0

浮动按值传递。您需要将返回值赋给回体面:

decent = velocity(decent); 

另一种选择是,因为它是一个成员变量,只是不把它传递给方法。在该方法中,您指的是传入的变量,而不是您的成员变量。

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

,然后离开你作为你现在拥有它。

+0

已更新,以包含进一步的深入信息。 – 2012-02-15 23:57:15