2011-07-08 33 views
0

我正在製作一個程序,裏面有很少的程序,我已經走到了一個兩難的境地。在我的第一個微型程序重新排列數字以從這些數字中找到最大可能的數字時,它會詢問用戶是否想要退出。如果他們回答「是」,則該函數返回0,該值在main(string [] args)方法中計算。我的問題是,無論何時用戶說「不」,小程序仍然不會繼續。下面是我的源:轉義一個函數返回Main()

namespace ACSL_Competition 
    { 
     class Program 
     { 
    static int DigitRearranger() 
    { 
     string[] mainString = {}; 
     Console.WriteLine("---------Welcome to the Digit Re-arranger!---------"); 
     Console.WriteLine("I take a positive number up to 10000, and find the highest number that can be made out of its digits."); 
     Console.WriteLine("Instructions: Enter a number up to 10000, and see the results!"); 
     drLabel: 
     Console.Write("Your Number: "); 

     string input = Console.ReadLine(); 
     int inputNumber = 0; 
     try { inputNumber = int.Parse(input); } 
     catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); goto drLabel; } 

     /*Placeholder code for the moment*/Console.WriteLine(inputNumber.ToString()); 
     evaluate: 
     Console.Write("Do you want to exit? Yes/No: "); 
     if (Console.ReadLine().Equals("Yes")) 
      return 1; 
     else if (Console.ReadLine().Equals("No")) 
     { 
      goto drLabel; 

     } 
     else 
     { 
      return 1; 
     } 

    } 
    static void Main(string[] args) 
    { 
     Console.WriteLine("Welcome to the ACSL Competition Program. Choose a program to begin:"); 
     Console.Write("\n\t"); 
     Console.WriteLine("1\tDigit Re-arranger"); 
     label: 
     Console.Write("\nProgram: "); 
     string input = Console.ReadLine(); 
     int number = 0; 
     try { number = int.Parse(input); } 
     catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message); goto label; } 
     if (number == 1) 
     { 
      Console.WriteLine("\n"); 
      if (DigitRearranger() == 1) 
      { 
       goto label; 
      } 
      else if (DigitRearranger() != 1) 
      { 
       DigitRearranger(); 
      } 
     } 
     else if (!number.Equals(1)) 
     { 
      Console.WriteLine("Not a valid program."); 
      goto label; 
     } 
     //---------------- 
     Console.ReadLine(); 
    } 
} 

}

+0

不要使用goto語句,重構代碼。 http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF。理想情況下,您應該「逃避」您的功能的唯一方法是在單獨和最終的退貨聲明。 – Jodrell

+0

您的參賽作品是否必須限制在控制檯窗口? – Jodrell

+0

我實際上並沒有進入比賽,我只是在練習中遇到問題,但我將它保存在控制檯中,這樣我就可以更少地關注用戶界面,更多關注邏輯。 – airplaneman19

回答

4

根本的問題是,我們在調用readline的兩倍。第一次獲得輸入值,即是,第二次調用它時,沒有要讀取的數據,所以它返回「」。如果你需要重複使用相同的輸入儲存於一個變量,即

string inputVal = Console.ReadLine(); 

我恨goto語句,也許你可以在調整你的代碼到一個while循環,是這樣的:

bool exit = false; 
while(!exit) 
{ 
    Console.Write("Your Number: "); 
    //Your main code 
    Console.Write("Do you want to exit? Yes/No: "); 
    if(Console.ReadLine() != "No") 
     exit = true; 
} 

在事實上,你可以擺脫退出變量,只是做(真),並返回如果用戶輸入任何非否。

0

我有幾個建議:

  1. 寫你的代碼更加模塊化,以提高可讀性。 Main()方法只應該驅動外部UI循環,每個模塊都提供它自己的UI。
  2. 切勿使用goto語句。
  3. 不要在if條件中使用Console.Readline()(當不是「Yes」時,它被調用兩次)。

這裏是我的代碼重構的的版本:

,使其沒有必要
class Program { 
    static void DigitRearranger() 
    { 
     string response = ""; 
     int num; 
     do 
     { 
      Console.Clear(); 
      Console.ForegroundColor = ConsoleColor.Yellow; 
      Console.WriteLine("---------Welcome to the Digit Re-arranger!---------"); 
      Console.WriteLine("I take a positive number up to 10000, and find the highest number that can be made out of its digits."); 
      Console.WriteLine("Instructions: Enter a number up to 10000, and see the results!"); 
      Console.ResetColor(); 

      Console.Write("Your Number: "); 
      if (!int.TryParse(Console.ReadLine(), out num)) 
      { 
       Console.WriteLine("Not a number. Press any key to continue"); 
       Console.ReadKey(); 
       continue; 
      } 
      //todo: reaarrange the number & print results 
      /*Placeholder code for the moment*/ 
      Console.WriteLine(num); 

      Console.Write("Do you want to exit? Yes/No: "); 
      response = Console.ReadLine(); 

     } while (response.ToLower() != "yes"); 
    } 

    //UI driver only in Main method: 
    static void Main(){ 
     string response = ""; 

     do 
     { 
      Console.Clear(); 
      Console.WriteLine("Welcome to the ACSL Competition Program. Choose a program to begin:"); 
      Console.WriteLine("\n\t1\tDigit Re-arranger"); 
      Console.WriteLine("\tq\tQuit"); 

      Console.Write("\nProgram: "); 

      response = Console.ReadLine(); 
      switch(response) 
      { 
       case "1": 
        DigitRearranger(); 
        break; 
       case "q": 
        break; 
       default: 
        Console.WriteLine("Not a valid program. Press any key to continue"); 
        Console.ReadKey(); 
        break; 
      } 
     } while (response.ToLower() != "q"); 
     Console.ReadLine(); 
    }} 
相關問題