2012-02-15 287 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