2012-10-25 55 views
-1

任何人都可以幫助我重新啓動我的程序從第46行,如果用戶輸入1(就在它說明下一個代碼將要求用戶2輸入的評論之後),如果用戶輸入-1結束它。我想不出如何去做。我是C#的新手,你可以給予的任何幫助都會很棒!用if語句從某一行重新啓動程序?

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

我不會幫助您直到您重構您的代碼,以便它使用方法並將控制邏輯從計算中分離出來。 – Euphoric

+0

程序重新啓動後,您是否想要從第46行開始執行程序? –

+0

我覺得我已經被推遲到Commodore Basic了。 GOTO不是前進的方向......瞭解方法的時間。 –

回答

1
bool exit = false; 
do 
{ 
    Console.WriteLine("Please enter the two points that you wish to know the distance between:"); 
    ... 
    Console.WriteLine("If you wish to calculate another distance type 1 and return, if you wish to end the program, type -1."); 

    string input; 

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

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

但你會做更好的思考關於將邏輯推入方法和功能中,這樣您的程序就由更小的構建塊組成。

你應該瞄向是這樣的:

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

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

這使得你的程序自我記錄的外環和方法自我解釋。

+0

Thankyou這是有益的,我會盡量讓它變成更小的積木。 – user1744093

0
if (Convert.ToInt16(Console.ReadLine()) == 1); 
{ 
    Main(args) 
} 
0

這是相當做了一個奇怪的事情。 你看過while循環嗎?

你可以把上面的結構和循環它的方法,而userinput等於1 如果它等於-1,調用下面的語句

Application.Exit() 
0

可以使用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; 
     } 
    } 

也做了一些代碼格式化。希望能幫助到你。

0

你可以用goto做到這一點,但請注意,這被認爲是不好的做法。

static void Main(string[] args) 
{ 
... 
MyLabel: 
... 
if (Convert.ToInt16(Console.ReadLine()) == 1); 
{ 
    //here is where I need it to repeat 
    goto MyLabel; 
}