转义函数以返回Main()

时间:2011-07-08 14:01:39

标签: c# .net return-value return main

我正在制作一个程序,其中包含很少的程序,我陷入了两难境地。在我的第一个重新排列数字以便从这些数字中找到最大数字的迷你程序中,它询问用户是否想要退出。如果它们回答“是”,则函数返回0,该函数在main(string [] args)方法中计算。我的问题是,只要用户说“不”,迷你程序仍然无法继续。这是我的来源:

    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();
    }
}

}

2 个答案:

答案 0 :(得分: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;
}

实际上,你可以摆脱退出变量,只需执行while(true)并返回,如果用户输入的内容不是no。

答案 1 :(得分:0)

我有一些建议:

  1. 将您的代码编写为更加模块化以提高可读性。 Main()方法应该只驱动外部UI循环,每个模块都提供自己的UI。
  2. 永远不要使用goto语句。
  3. 不要在if条件中使用Console.Readline()(当不是“是”时,它被调用两次)。
  4. 以下是我重构的代码版本:

        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();
        }}