0

I am trying to pass a variable to a method, i.e. velocity(decent), and call that method several times in a while-loop.

It seams to not be working and I don't know what is wrong with this.

I want the decent variable to increase up to 250.

I tested the velocity(decent) method outside of the while loop and it worked. Inside the while-loop it just stays 0, while the altitude is decreasing each time it goes though the while loop.

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

// Every measuerments are in feet and 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
    }
}
7
  • 3
    I don't see any question mark. Commented Feb 15, 2012 at 23:38
  • 1
    How comes you are "trying to pass a global variable to a method" while global variables don't exist at all in C#? Commented Feb 15, 2012 at 23:38
  • You are not allocating the return value of the velocity method to a variable. The variable you pass into the method cannot be altered from within the method because it is a value type. Commented Feb 15, 2012 at 23:38
  • 1
    @Kriz: static vars come pretty close. Commented Feb 15, 2012 at 23:49
  • @Krizz who told you that "global variables don't exist at all in C#"? Ofcouese it exists. You can declare a static variable and it'll surely work as global variable in C#. Commented Feb 16, 2012 at 8:03

3 Answers 3

3

I think the problem your seeing is that you expect your decent = decent + 31.25f; in velocity to set the value of your class's field defined as private static float decent;.

The problem with this is since your parameter is also named decent it overrides the meaning of your broader scoped decent while in the context of velocity. In order to achieve this effect, you could either rename the decent parameter to something else or use:

this.decent = decent + 31.25f

I don't quite understand why decent is a field though if you want to pass it to a member method.

It is better to change the declaration of velocity to:

private static void velocity()

and then leave your use as you currently have it.

Sign up to request clarification or add additional context in comments.

1 Comment

Updated to include further insight.
2

I think you want :

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

And that also means that decent (descent) does not have to be a global variable. It could become a proper local of Main(). Do try to avoid globals as a first step to writing better software.

Also try

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

for some dazzling video effects.

Comments

2

Floats are passed by value. You need to assign the return value back into decent:

decent = velocity(decent);

The better option is (that since it is a field) just don't pass it to the method at all.

private static void velocity();

Just instead refer directly to the field decent inside the method velocity.

Now inside your method, you're referring to the variable passed in decent, not to the field decent (both are named the same).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.