Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 转义函数以返回Main()_C#_.net_Return Value_Return_Main - Fatal编程技术网

C# 转义函数以返回Main()

C# 转义函数以返回Main(),c#,.net,return-value,return,main,C#,.net,Return Value,Return,Main,我正在制作一个里面有小程序的程序,我陷入了两难的境地。在我的第一个迷你程序中,它重新排列数字,从这些数字中找出最大可能的数字,它询问用户是否想退出。如果它们响应“Yes”,则函数返回0,该值在main(string[]args)方法中计算。我的问题是,每当用户说“不”,迷你程序仍然无法继续。以下是我的消息来源: namespace ACSL_Competition { class Program { static int DigitRear

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

}

根本问题是您要调用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;
}

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

根本问题是您调用了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;
}

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

我有一些建议:

  • 编写更模块化的代码以提高可读性。Main()方法应该只驱动外部UI循环,每个模块都提供自己的UI
  • 永远不要使用goto语句
  • 不要在if条件中使用Console.Readline()
  • 以下是我的代码重构版本:

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

    我有几点建议:

  • 编写更模块化的代码以提高可读性。Main()方法应该只驱动外部UI循环,每个模块都提供自己的UI
  • 永远不要使用goto语句
  • 不要在if条件中使用Console.Readline()
  • 以下是我的代码重构版本:

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

    不要使用goto,重构代码,这样就没有必要了。理想情况下,你应该“逃离”你的函数的唯一方法是在单独的和最终的返回语句中。你的比赛条目必须限制在控制台窗口吗?我不是真的进入比赛,我只是在练习问题,但我把它放在控制台中,这样我就可以少关注UI,多关注逻辑。不要使用goto,重构代码,使其不再需要。理想情况下,你应该“逃离”你的函数的唯一方法是在单独的和最终的返回语句中。你的比赛条目必须限制在控制台窗口吗?我不是真正进入比赛,我只是在练习问题,但我把它放在控制台中,这样我就可以少关注UI,多关注逻辑。