C# 使用if语句从某一行重新启动程序?

C# 使用if语句从某一行重新启动程序?,c#,if-statement,repeat,C#,If Statement,Repeat,如果用户输入1(就在注释之后,它声明下一个代码将要求用户输入2个输入),并且如果用户输入-1结束,有人能帮我从第46行重新启动程序吗。我想不出怎么做。我是新来C#你能给我任何帮助都会很棒 class Program { static void Main(string[] args) { //Displays data in correct Format List<float> inputList = new List<floa

如果用户输入1(就在注释之后,它声明下一个代码将要求用户输入2个输入),并且如果用户输入-1结束,有人能帮我从第46行重新启动程序吗。我想不出怎么做。我是新来C#你能给我任何帮助都会很棒

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    


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

        Coordinates distance = new Coordinates();

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

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


        //Calculate the distance between two points

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

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

        double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos((distance.latitude) * (PIx / 180)) * Math.Cos((distance.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);

        Console.WriteLine("The distance between your two points is...");
        Console.WriteLine(ultimateDistance);

        //Repeat the program if the user enters 1, end the program if the user enters -1

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

        if (Convert.ToInt16(Console.ReadLine()) == 1);

        {
           //here is where I need it to repeat
        }
类程序
{
静态void Main(字符串[]参数)
{
//以正确的格式显示数据
List inputList=新列表();
TextReader tr=new StreamReader(“c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt”);
字符串输入=Convert.ToString(tr.ReadToEnd());
String[]items=input.Split(',');
控制台写线(“点纬度纵向高程”);
对于(int i=0;i
这样做很奇怪。 你看过while循环了吗

当userinput等于1时,您可以使用上述结构创建一个方法并循环它 如果它等于-1,则调用以下语句

Application.Exit()
但您最好考虑将逻辑推入方法和函数中,以便您的程序由更小的构建块构建

你应该瞄准这样的目标:

bool exit = false;
do
{
    Point[] points = ReadCoordinates();
    Whatever result = CalculateWhatever();
    DisplayResults(results);

    exit = ShouldExit();
}
while (!exit);

这使得程序的外部循环可以自我记录,方法也可以自我解释。

可以使用Goto在用户输入时使程序循环

    public static void Main(string[] args)
    {
        RestartApplication:

        //// Displays data in correct Format
        TextReader textReader = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
        var input = Convert.ToString(textReader.ReadToEnd());
        var items = input.Split(',');
        Console.WriteLine("Point         Latitude        Longtitude       Elevation");

        for (var 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 transferred in class Coordinates    
        Console.WriteLine("Please enter the two points that you wish to know the distance between:");
        var point = Console.ReadLine();
        string[] pointInput;
        if (point != null)
        {
            pointInput = point.Split(' ');
        }
        else
        {
            goto RestartApplication;
        }

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

        var distance = new Coordinates
            {
                Latitude = Convert.ToDouble(items[pointNumber * 3]),
                Longtitude = Convert.ToDouble(items[(pointNumber * 3) + 1]),
                Elevation = Convert.ToDouble(items[(pointNumber * 3) + 2]),
                Latitude2 = Convert.ToDouble(items[pointNumber2 * 3]),
                Longtitude2 = Convert.ToDouble(items[(pointNumber2 * 3) + 1]),
                Elevation2 = Convert.ToDouble(items[(pointNumber2 * 3) + 2])
            };

        //// Calculate the distance between two points
        const double PIx = 3.141592653589793;
        const double Radio = 6371;

        var dlat = (distance.Latitude2 * (PIx / 180)) - (distance.Latitude * (PIx / 180));
        var dlon = (distance.Longtitude2 * (PIx / 180)) - (distance.Longtitude * (PIx / 180));

        var a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(distance.Latitude * (PIx / 180)) * Math.Cos(distance.Latitude2 * (PIx / 180)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
        var angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        var ultimateDistance = angle * Radio;
        Console.WriteLine("The distance between your two points is...");
        Console.WriteLine(ultimateDistance);

        //// Repeat the program if the user enters 1, end the program if the user enters -1
        Console.WriteLine("If you wish to calculate another distance type 1 and return, if you wish to end the program, type -1.");
        var userInput = Console.ReadLine();
        if (Convert.ToInt16(userInput) == 1)
        {
            //// Here is where the application will repeat.
            goto RestartApplication;
        }
    }
publicstaticvoidmain(字符串[]args)
{
重新启动应用程序:
////以正确的格式显示数据
TextReader TextReader=newstreamReader(“c:/users/tom/documents/visualstudio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt”);
var input=Convert.ToString(textReader.ReadToEnd());
var items=input.Split(',');
控制台写线(“点纬度纵向高程”);
对于(变量i=0;ibool exit = false;
do
{
    Point[] points = ReadCoordinates();
    Whatever result = CalculateWhatever();
    DisplayResults(results);

    exit = ShouldExit();
}
while (!exit);
    public static void Main(string[] args)
    {
        RestartApplication:

        //// Displays data in correct Format
        TextReader textReader = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
        var input = Convert.ToString(textReader.ReadToEnd());
        var items = input.Split(',');
        Console.WriteLine("Point         Latitude        Longtitude       Elevation");

        for (var 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 transferred in class Coordinates    
        Console.WriteLine("Please enter the two points that you wish to know the distance between:");
        var point = Console.ReadLine();
        string[] pointInput;
        if (point != null)
        {
            pointInput = point.Split(' ');
        }
        else
        {
            goto RestartApplication;
        }

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

        var distance = new Coordinates
            {
                Latitude = Convert.ToDouble(items[pointNumber * 3]),
                Longtitude = Convert.ToDouble(items[(pointNumber * 3) + 1]),
                Elevation = Convert.ToDouble(items[(pointNumber * 3) + 2]),
                Latitude2 = Convert.ToDouble(items[pointNumber2 * 3]),
                Longtitude2 = Convert.ToDouble(items[(pointNumber2 * 3) + 1]),
                Elevation2 = Convert.ToDouble(items[(pointNumber2 * 3) + 2])
            };

        //// Calculate the distance between two points
        const double PIx = 3.141592653589793;
        const double Radio = 6371;

        var dlat = (distance.Latitude2 * (PIx / 180)) - (distance.Latitude * (PIx / 180));
        var dlon = (distance.Longtitude2 * (PIx / 180)) - (distance.Longtitude * (PIx / 180));

        var a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(distance.Latitude * (PIx / 180)) * Math.Cos(distance.Latitude2 * (PIx / 180)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
        var angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        var ultimateDistance = angle * Radio;
        Console.WriteLine("The distance between your two points is...");
        Console.WriteLine(ultimateDistance);

        //// Repeat the program if the user enters 1, end the program if the user enters -1
        Console.WriteLine("If you wish to calculate another distance type 1 and return, if you wish to end the program, type -1.");
        var userInput = Console.ReadLine();
        if (Convert.ToInt16(userInput) == 1)
        {
            //// Here is where the application will repeat.
            goto RestartApplication;
        }
    }
static void Main(string[] args)
{
...
MyLabel:
...
if (Convert.ToInt16(Console.ReadLine()) == 1);
{
    //here is where I need it to repeat
    goto MyLabel;
}