转义函数以返回Main()

本文关键字:Main 返回 函数 转义 | 更新日期: 2023-09-27 18:01:29

我正在编写一个程序,里面有一些小程序,我陷入了两难境地。在我的第一个小程序中,它重新排列数字以从这些数字中找到可能的最大数字,它询问用户是否要退出。如果它们响应"是",则函数返回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();
    }
}

}

转义函数以返回Main()

潜在的问题是您调用了两次readline。第一次它获得输入的值,即Yes,第二次调用它时没有数据可读,所以它返回"。如果您需要重用相同的输入,将其存储在变量中,即

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

实际上,您可以去掉exit变量,只需执行while(true)并在用户输入除no以外的任何内容时返回。

我有一些建议:

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