4

私は自分自身にC#を教えていますが、現在の章の課題では次のように求められました。

最後のプロジェクトを取得し、渡された2つの数値を減算、乗算、または除算する追加のメソッドを作成します。除算方法では、0で除算することは違法な数学的概念であるため、2番目の数値が0でないことを確認してください。2番目の数値が0の場合は、0を返します。

今、私はすべての基準を満たしていると私が信じる以下を書きました。IFステートメントが最良の選択であるかどうかはわかりませんでしたが、機能しました。また、SWITCHでもうまくいくと思いました。

では、最初の質問ですが、IFまたはSWITCHの方が良かったでしょうか。

2番目の質問。ELSEでは、ユーザーが使用可能なオプションの1つを選択しなかった場合に一般的な失敗メッセージを表示しますが、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の主な制限は、コンパイル時の定数値(const変数、リテラル文字列、int、enumなど)でのみ機能することです。この場合、それは問題ではないように思われるので、スイッチはおそらく、よりクリーンでわずかに短いコードに適しています。パフォーマンスに敏感なコード(これは確かにそうではありません)では、プログラムが値をテストし、一致するまで各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 に答える