嘿我想將一個全局變量傳遞給一個方法,並在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
}
}
我沒有看到任何問號。 – 2012-02-15 23:38:45
你是如何「嘗試將全局變量傳遞給方法」而全局變量*在C#中根本不存在? – Krizz 2012-02-15 23:38:49
您不會將'velocity'方法的返回值分配給一個變量。您傳遞給方法的變量不能在方法內部更改,因爲它是一個值類型。 – adrianbanks 2012-02-15 23:38:56