0

出于某种原因,这会编译,但最后会出现错误,我不知道为什么。代码的第一部分是显示文本文件中正常工作的表格,第二部分没有。

我认为它甚至没有到达 Console.WriteLine 位,这是一种检查它是否存在的方法。谁能明白为什么?

谢谢你提供的所有帮助!

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

    {
        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 bits of data which are then stored in Longtitude, Latitude and Elevation 

        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]);

        int Latitude = (Convert.ToInt16(items[pointNumber*3]));
        int Longtitude = (Convert.ToInt16(items[(pointNumber*3)+1]));
        int Elevation = (Convert.ToInt16(items[(pointNumber*3)+2]));

        int Latitude2 = (Convert.ToInt16(items[pointNumber2 * 3]));
        int Longtitude2 = (Convert.ToInt16(items[(pointNumber2 * 3) + 1]));
        int Elevation2 = (Convert.ToInt16(items[(pointNumber2 * 3) + 2]));

        Console.WriteLine("Latitude");
        Console.WriteLine("Latitude2");
4

2 回答 2

1

You are using decimal values, which cannot be converted into Int16. So use float.

Also, Outputting "Latitude" will write the variable's name, not its value. I modified your code:

float Latitude = (float.Parse(items[pointNumber*3]));
float Longtitude = (float.Parse(items[(pointNumber*3)+1]));
float Elevation = (float.Parse(items[(pointNumber*3)+2]));

float Latitude2 = (float.Parse(items[pointNumber2 * 3]));
float Longtitude2 = (float.Parse(items[(pointNumber2 * 3) + 1]));
float Elevation2 = (float.Parse(items[(pointNumber2 * 3) + 2]));

Console.WriteLine(Latitude);
Console.WriteLine(Latitude2);
于 2012-10-24T17:18:22.367 回答
1

Your input strings are of floating values, not integers. You can parse to floats like this:

float Latitude = (Convert.ToSingle(items[pointNumber * 3]));
float Longtitude = (Convert.ToSingle(items[(pointNumber * 3) + 1]));
float Elevation = (Convert.ToSingle(items[(pointNumber * 3) + 2]));

float Latitude2 = (Convert.ToSingle(items[pointNumber2 * 3]));
float Longtitude2 = (Convert.ToSingle(items[(pointNumber2 * 3) + 1]));
float Elevation2 = (Convert.ToSingle(items[(pointNumber2 * 3) + 2]));
于 2012-10-24T17:18:41.323 回答