C# 返回程序顶部重试

C# 返回程序顶部重试,c#,C#,我在自学C#,而当前的章节挑战要求我: 以您的上一个项目为例,创建额外的方法,对传入的两个数字进行减法、乘法或除法。在divide方法中,检查第二个数字是否不是0,因为除以0是非法的数学概念。如果第二个数字是0,只需返回0即可 现在我写了下面我认为符合所有标准的文章。我不确定if语句是否是最佳选择,但它确实起了作用。我还以为换一个开关也能奏效 所以,第一个问题是,如果或切换会更好吗 第二个问题。在ELSE中,如果用户没有选择其中一个可用选项,我会给出一条通用的失败消息,但我想做的是,如果调用了E

我在自学C#,而当前的章节挑战要求我:

以您的上一个项目为例,创建额外的方法,对传入的两个数字进行减法、乘法或除法。在divide方法中,检查第二个数字是否不是0,因为除以0是非法的数学概念。如果第二个数字是0,只需返回0即可

现在我写了下面我认为符合所有标准的文章。我不确定if语句是否是最佳选择,但它确实起了作用。我还以为换一个开关也能奏效

所以,第一个问题是,如果或切换会更好吗

第二个问题。在ELSE中,如果用户没有选择其中一个可用选项,我会给出一条通用的失败消息,但我想做的是,如果调用了ELSE(不确定正确的术语是什么),我希望程序返回到开始位置,并要求用户重试并显示第一个控制台。Writeline()要求选择运算符。我知道这不是挑战的一部分,但它似乎是对程序的合理补充,我想知道这是否可能,而不需要诉诸任何太复杂的手段

提前谢谢

        string whichOp;
        int firstNum, secondNum, result;

        Console.WriteLine("What Operator do you wish to use? [A]dd, [S]ubtract, [M]ultiply or [D]ivide?");

        whichOp = Console.ReadLine();

        whichOp = whichOp.ToLower();

        if (whichOp == "a")
        {
            Console.Write("You chose Addition.  Please choose your first number: ");
            firstNum = int.Parse(Console.ReadLine());
            Console.Write("You chose the number {0}.  Please choose a second number: ", firstNum);
            secondNum = int.Parse(Console.ReadLine());
            result = Add(firstNum, secondNum);
            Console.WriteLine("You chose the number {0}.  {1} plus {2} equals {3}.", secondNum, firstNum, secondNum, result);
        }
        else if (whichOp == "s")
        {
            Console.Write("You chose Subtraction.  Please choose your first number: ");
            firstNum = int.Parse(Console.ReadLine());
            Console.Write("You chose the number {0}.  Please choose a second number: ", firstNum);
            secondNum = int.Parse(Console.ReadLine());
            result = Sub(firstNum, secondNum);
            Console.WriteLine("You chose the number {0}.  {1} minus {2} equals {3}.", secondNum, firstNum, secondNum, result);
        }
        else if (whichOp == "m")
        {
            Console.Write("You chose Multiplication.  Please choose your first number: ");
            firstNum = int.Parse(Console.ReadLine());
            Console.Write("You chose the number {0}.  Please choose a second number: ", firstNum);
            secondNum = int.Parse(Console.ReadLine());
            result = Mult(firstNum, secondNum);
            Console.WriteLine("You chose the number {0}.  {1} times {2} equals {3}.", secondNum, firstNum, secondNum, result);
        }
        else if (whichOp == "d")
        {
            Console.Write("You chose Division.  Please choose your first number: ");
            firstNum = int.Parse(Console.ReadLine());
            Console.Write("You chose the number {0}.  Please choose a second number: ", firstNum);
            secondNum = int.Parse(Console.ReadLine());
            result = Div(firstNum, secondNum);
            Console.WriteLine("You chose the number {0}.  {1} divided by {2} equals {3}.", secondNum, firstNum, secondNum, result);
        }
        else
            Console.WriteLine("FAIL!  You did not choose an available option.");

        Console.ReadLine();
    }

    static int Add(int num1, int num2)
    {
        int theAnswer;

        theAnswer = num1 + num2;

        return theAnswer;
    }

    static int Mult(int num1, int num2)
    {
        int theAnswer;

        theAnswer = num1 * num2;

        return theAnswer;
    }

    static int Sub(int num1, int num2)
    {
        int theAnswer;

        theAnswer = num1 - num2;

        return theAnswer;
    }

    static int Div(int num1, int num2)
    {
        int theAnswer;

        if (num2 == 0)
            return 0;

        theAnswer = num1 / num2;

        return theAnswer;
    }
编辑:我采纳了这里的建议,用SWITCH和WHILE重建了程序。有人还说,因为很多代码是相同的,我应该能够重用它。我喜欢这个想法,并将研究如何做到这一点

        var retry = true;
        while (retry)
        {
            retry = false;

            string whichOp;
            int firstNum, secondNum, result;

            Console.WriteLine("What Operator do you wish to use? [A]dd, [S]ubtract, [M]ultiply or [D]ivide?");

            whichOp = Console.ReadLine();

            whichOp = whichOp.ToLower();

            switch (whichOp)
            {
                case "a":
                    Console.Write("You chose Addition.  Please choose your first number: ");
                    firstNum = int.Parse(Console.ReadLine());
                    Console.Write("You chose the number {0}.  Please choose a second number: ", firstNum);
                    secondNum = int.Parse(Console.ReadLine());
                    result = Add(firstNum, secondNum);
                    Console.WriteLine("You chose the number {0}.  {1} plus {2} equals {3}.", secondNum, firstNum, secondNum, result);
                    Console.ReadLine();
                    break;
                case "s":
                    Console.Write("You chose Subtraction.  Please choose your first number: ");
                    firstNum = int.Parse(Console.ReadLine());
                    Console.Write("You chose the number {0}.  Please choose a second number: ", firstNum);
                    secondNum = int.Parse(Console.ReadLine());
                    result = Sub(firstNum, secondNum);
                    Console.WriteLine("You chose the number {0}.  {1} minus {2} equals {3}.", secondNum, firstNum, secondNum, result);
                    Console.ReadLine();
                    break;
                case "m":
                    Console.Write("You chose Multiplication.  Please choose your first number: ");
                    firstNum = int.Parse(Console.ReadLine());
                    Console.Write("You chose the number {0}.  Please choose a second number: ", firstNum);
                    secondNum = int.Parse(Console.ReadLine());
                    result = Mult(firstNum, secondNum);
                    Console.WriteLine("You chose the number {0}.  {1} times {2} equals {3}.", secondNum, firstNum, secondNum, result);
                    Console.ReadLine();
                    break;
                case "d":
                    Console.Write("You chose Division.  Please choose your first number: ");
                    firstNum = int.Parse(Console.ReadLine());
                    Console.Write("You chose the number {0}.  Please choose a second number: ", firstNum);
                    secondNum = int.Parse(Console.ReadLine());
                    result = Div(firstNum, secondNum);
                    Console.WriteLine("You chose the number {0}.  {1} divided by {2} equals {3}.", secondNum, firstNum, secondNum, result);
                    Console.ReadLine();
                    break;
                default:
                    Console.WriteLine("I'm sorry.  {0} is not an available option.  Please try again.", whichOp.ToUpper());
                    retry = true;
                    break;
            }
        }
    }

    static int Add(int num1, int num2)
    {
        int theAnswer;

        theAnswer = num1 + num2;

        return theAnswer;
    }

    static int Mult(int num1, int num2)
    {
        int theAnswer;

        theAnswer = num1 * num2;

        return theAnswer;
    }

    static int Sub(int num1, int num2)
    {
        int theAnswer;

        theAnswer = num1 - num2;

        return theAnswer;
    }

    static int Div(int num1, int num2)
    {
        int theAnswer;

        if (num2 == 0)
            return 0;

        theAnswer = num1 / num2;

        return theAnswer;
    }
(1) switch通常是表示这种分支的更简洁的方式。switch的主要限制是它只对编译时常量值(常量变量、文本字符串、整数、枚举等)起作用。在本例中,这似乎不是一个问题,所以对于更干净、稍短的代码,switch可能是一个不错的选择。在对性能敏感的代码中(这当然不是),单个开关可以比一堆ifs更快,因为程序将测试值并直接跳到正确的情况,而不是针对每个if条件进行测试,直到达到匹配

(2) 一种简单的方法是将整个程序包装在一个循环中:

var retry = true;
while (retry)
    retry = false;
    // your program
    else { // or default: if you're going with switch
        ...
        retry = true;
    }
}

您上次在机器上使用要求您键入a、S或M的程序是什么时候?然后告诉你你失败了!如果你打了别的什么?为什么你不让你的程序更像你每天使用的程序?@HansPassant就像一个图形计算器应用程序?“对于初学者来说,这要高级一点。”柯克·布罗德赫斯特说得没错。我知道它很难看,而且是一个90年代风格的程序,但它是一本使用控制台应用程序的初学者C#book。我认为这是在进入UIs之前教授基础知识,我所看到的几乎每一本初学者程序书都是这样做的。当然,在C#做一名初学者是很好的。这并不意味着你必须对你的用户粗鲁。你为什么不直接说“哎呀!,我不知道你的意思然后键入有效的命令?@HansPassant如果你认为这很粗鲁,你应该试试我的一些控制台应用程序。定义仅限于成人。此外,每个区块的作用几乎相同:读取2个数字并应用一个操作。您应该能够重用其中的大部分。只把
result=…
行放在开关中。@ColeJohnson goto应该始终是控制程序流的最后手段。如果你能用基本的If,while,switch语句实现你所需要的控制,而不破坏宇宙,那么在gotoA循环之前应该考虑的那些语句比goto更性感