4

我正在自学 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;
    }
4

1 回答 1

4

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

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

var retry = true;
while (retry)
    retry = false;
    // your program
    else { // or default: if you're going with switch
        ...
        retry = true;
    }
}
于 2012-09-14T01:06:35.713 回答