返回程序顶部再试一次

时间:2012-09-14 00:59:45

标签: c#

我正在教自己C#,当前的章节挑战让我:

获取您的上一个项目并创建其他方法,减去,乘以或除以传递给它的两个数字。在除法中,检查第二个数不是0,因为除以0是非法的数学概念。如果第二个数字是0,则返回0。

现在我写下了我认为符合所有标准的内容。我不确定IF声明是否是最佳选择,但确实有效。我还认为SWITCH也可以做到这一点。

第一个问题,IF或SWITCH会更好吗?

第二个问题。在ELSE中,如果用户没有选择其中一个可用选项,我会给出一个通用的失败消息,但我想要做的是调用ELSE(不确定正确的术语是什么),我希望程序能够回到开始并要求用户再次尝试并显示第一个要求选择运营商的Console.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 个答案:

答案 0 :(得分:4)

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

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

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