0

私は C# を初めて使用するので、コードの形式が間違っていたら申し訳ありません。現在、データの距離を計算するクラスから値を返す方法を考え出そうとしています。

ここに私がこれまでに持っているものがありますが、1つまたは2つのポイントを尋ねると、唯一出てくるのはDistanceGenerator.Courseworkです。私が間違っていることはわかりません。何か助けはありますか?

namespace DistanceGenerator
{
    class Program
    {
        static void Main(string[] args)
        {

            //Displays data in correct Format

            List<float> inputList = new List<float>();
            TextReader tr = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
            String input = Convert.ToString(tr.ReadToEnd());
            String[] items = input.Split(',');
            Console.WriteLine("Point         Latitude        Longtitude       Elevation");

            for (int i = 0; i < items.Length; i++)
            {
                if (i % 3 == 0)
                {
                    Console.Write((i / 3) + "\t\t");
                }

                Console.Write(items[i]);
                Console.Write("\t\t");

                if (((i - 2) % 3) == 0)
                {
                    Console.WriteLine();
                }

            }

            Console.WriteLine();
            Console.WriteLine();

            // Ask for two inputs from the user which is then converted into 6 floats and transfered in class Coordinates

            bool exit = false;
            do
            {

                Console.WriteLine("Please enter the two points that you wish to know the distance between:");
                string point = Console.ReadLine();
                string[] pointInput = point.Split(' ');

                int pointNumber = Convert.ToInt16(pointInput[0]);
                int pointNumber2 = Convert.ToInt16(pointInput[1]);

               double latitude = (Convert.ToDouble(items[pointNumber * 3]));
               double longtitude = (Convert.ToDouble(items[(pointNumber * 3) + 1]));
               double elevation = (Convert.ToDouble(items[(pointNumber * 3) + 2]));

               double latitude2 = (Convert.ToDouble(items[pointNumber2 * 3]));
               double longtitude2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 1]));
               double elevation2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 2]));


                //Calculate the distance between two points
                Distance curDistance = new Distance(latitude, longtitude, elevation, latitude2, longtitude2, elevation2);

                Console.WriteLine(curDistance);


                Console.WriteLine("If you wish to calculate another distance type 1 and return, if you wish to end the program, type -1.");

                string reset;

                do
                {
                    reset = Console.ReadLine().Trim();
                }
                while (reset != "1" && reset != "-1");

                if (reset == "-1") exit = true;
            }
            while (!exit);

        }
    }
}

//そしてここにクラスがあります

namespace DistanceGenerator
{
    class Distance
    {
        private double latitude;
        private double longtitude;
        private double elevation;
        private double latitude2;
        private double longtitude2;
        private double elevation2;

        public Distance(double latitude, double longtitude, double elevation, double latitude2, double longtitude2, double elevation2)
        {
            // TODO: Complete member initialization
            this.latitude = latitude;
            this.longtitude = longtitude;
            this.elevation = elevation;
            this.latitude2 = latitude2;
            this.longtitude2 = longtitude2;
            this.elevation2 = elevation2;
        }

        private double curDistance()

        {

            const double PIx = 3.141592653589793;
            const double RADIO = 6371;

            double dlat = ((latitude2) * (PIx / 180)) - ((latitude) * (PIx / 180));
            double dlon = ((longtitude2) * (PIx / 180)) - ((longtitude) * (PIx / 180));

            double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos((latitude) * (PIx / 180)) * Math.Cos((latitude2) * (PIx / 180)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
            double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double ultimateDistance = (angle * RADIO);

            return ultimateDistance;

        }

    }
}
4

1 に答える 1

6

curDistanceあなたのMainメソッドでは、クラスのインスタンスです。Distance

クラスのcurDistnace メソッドは であり、クラス外では使用できなくなります - あなたはそれを作ることができます:Distnaceprivatepublic

public double curDistance()

クラスを出力するのではなく、計算の値を出力します。そのためには、インスタンスでメソッド を呼び出す必要があります (紛らわしいことに という名前も付けました)。curDistanceDistancecurDistance

Console.WriteLine(curDistance.curDistance());
于 2012-10-26T19:53:35.087 に答える