我想做的是使用 try-catch 异常重新启动程序并让用户再次重新输入数据值。我该怎么做?我尝试使用 goto 将其带回第一行,但这似乎不起作用。(普遍的共识是 goto 是邪恶的)。非常感谢任何可以提供的帮助。
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]); //Stores the actual input number's into two integers
int pointNumber2 = Convert.ToInt16(pointInput[1]);
try //Try-Catch statement to make sure that the User enters relevant PointNumbers
{
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])); // Uses the relationship between the pointnumber and the array to select the required items from the array.
//Calculate the distance between two point using the Distance class
Console.WriteLine("The distance in km's to two decimal places is:");
Distance curDistance = new Distance(latitude, longtitude, elevation, latitude2, longtitude2, elevation2);
Console.WriteLine(String.Format("{0:0.00}", curDistance.toDistance()) + "km");
}
catch(IndexOutOfRangeException)
{
Console.WriteLine("You have selected a point number outside the range of the data entered, please select two new pointnumbers");
// here is where I would have the program restart
}