Search code examples
c#methodsparameter-passingfieldreturn-value

Passing a variable to method and using the method return value in C#


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
    }
}

Solution

  • 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.