转义函数以返回到 Main()

发布于 2024-11-19 00:23:31 字数 2265 浏览 2 评论 0原文

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

}

I'm making a program that has little programs inside of it, and I've come to a dilemma. On my first mini-program which rearranges digits to find the greatest possible number from those digits, it asks if the user wants to quit. If they respond "Yes" then the function returns 0 which is evaluated in the main(string[] args) method. My problem is that whenever the user says "No", the mini-program still doesn't continue. Here's my source:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别闹i 2024-11-26 00:23:31

根本问题是您调用 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 之外的任何内容,则返回。

The underlying problem is that you're calling readline twice. The first time it gets the entered value, i.e. Yes, the second time you call it there is no data to read so it returns "". If you need to reuse the same input store it in a variable, i.e.

 string inputVal = Console.ReadLine();

I hate goto statements, maybe you could restructure your code in to a while loop, something like:

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

In fact, you could get rid of the exit variable, just do while(true) and return if the user enters anything other than no.

时光匆匆的小流年 2024-11-26 00:23:31

我有一些建议:

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

I have a few suggestions:

  1. Write your code to be more modular to improve readability. The Main() method should only drive the outer UI loop, each module provides its own UI.
  2. Never use goto statements.
  3. Don't use Console.Readline() inside an if condition (when not "Yes", it was called twice).

Here is my refactored version of your code:

    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();
    }}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文