返回程序的顶部再试一次 [英] Returning to top of program to try again

查看:105
本文介绍了返回程序的顶部再试一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自学C#和当前章节挑战问我:



把你的最后一个项目,并创建减,乘其它方法或除通过两个数字进去。在除法方法,检查第二数目不为0,因为除以0是非法的数学概念。如果第二个数字是0,只返回回0。



现在我写了下面的,我相信满足所有的标准。我不知道,如果一个IF语句是最好的选择,但它的工作。我也认为,一个开关会也做的伎俩。



所以第一个问题,会if或switch好些?



第二个问题。在ELSE,我给一个通用的失败消息,如果用户不选择可用选项之一,但我想这样做是如果else被称为(不知道正确的说法是什么),我想程序回到起点,并要求用户再次尝试并显示第一个Console.WriteLine()要求选择一个操作员。我知道这不是挑战的一部分,但它似乎是一个逻辑除了节目,想知道这是可能的,而不诉诸任何东西太复杂了。



< !p>在此先感谢

 字符串whichOp; 
INT firstNum,secondNum,结果;

Console.WriteLine(?算什么,你想用[A] DD,[S] ubtract,[M] ultiply或[D]。ivide?);

whichOp =到Console.ReadLine();

whichOp = whichOp.ToLower();

如果(whichOp ==A)
{
Console.Write(你选择加法请选择您的第一个数字:);
firstNum = int.Parse(到Console.ReadLine());
Console.Write(你选择了数{0}请选择第二个数字:firstNum);
secondNum = int.Parse(到Console.ReadLine());
结果=添加(firstNum,secondNum);
Console.WriteLine(,secondNum,firstNum,secondNum,结果你选择了数{0} {1}加上{2}等于{3}。);
}
,否则如果(whichOp ==S)
{
Console.Write(你选择了减法请选择您的第一个数字:);
firstNum = int.Parse(到Console.ReadLine());
Console.Write(你选择了数{0}请选择第二个数字:firstNum);
secondNum = int.Parse(到Console.ReadLine());
结果=子(firstNum,secondNum);
Console.WriteLine(你选择了数{0} {1}减去{2}等于{3}。secondNum,firstNum,secondNum,结果);
}
,否则如果(whichOp ==M)
{
Console.Write(你选择乘请选择您的第一个数字:);
firstNum = int.Parse(到Console.ReadLine());
Console.Write(你选择了数{0}请选择第二个数字:firstNum);
secondNum = int.Parse(到Console.ReadLine());
结果= MULT(firstNum,secondNum);
Console.WriteLine(,secondNum,firstNum,secondNum,结果你选择了数{0} {1}次{2}等于{3}。);
}
,否则如果(whichOp ==D)
{
Console.Write(你选择了科请选择您​​的第一个数字:);
firstNum = int.Parse(到Console.ReadLine());
Console.Write(你选择了数{0}请选择第二个数字:firstNum);
secondNum = int.Parse(到Console.ReadLine());
结果=股利(firstNum,secondNum);
Console.WriteLine(你选择了数{0} {1} {2}等于{3}分。secondNum,firstNum,secondNum,结果);
}
,否则
Console.WriteLine(FAIL你没有选择一个可用的选择!);

到Console.ReadLine();
}

静态INT添加(INT NUM1,NUM2 INT)
{
INT theAnswer;

theAnswer = NUM​​1 + NUM2;

返回theAnswer;
}

静态INT MULT(INT NUM1,NUM2 INT)
{
INT theAnswer;

theAnswer = NUM​​1 * NUM2;

返回theAnswer;
}

静态INT子(INT NUM1,NUM2 INT)
{
INT theAnswer;

theAnswer = NUM​​1 - NUM2;

返回theAnswer;
}

静态中期息(INT NUM1,NUM2 INT)
{
INT theAnswer;

如果(NUM2 == 0)
返回0;

theAnswer = NUM​​1 / NUM2;

返回theAnswer;
}



编辑:我把那些建议在这里和SWITCH重建程序,而。有人还表示,由于大量的代码是一样的,我应该能够重新使用它。我喜欢这个想法,并会考虑我怎么能做到这一点。



  VAR重试= TRUE; 
,而(重试)
{
重试= FALSE;

串whichOp;
INT firstNum,secondNum,结果;

Console.WriteLine(?算什么,你想用[A] DD,[S] ubtract,[M] ultiply或[D]。ivide?);

whichOp =到Console.ReadLine();

whichOp = whichOp.ToLower();

开关(whichOp)
{
字母a:
Console.Write(你选择加法请选择您的第一个数字:);
firstNum = int.Parse(到Console.ReadLine());
Console.Write(你选择了数{0}请选择第二个数字:firstNum);
secondNum = int.Parse(到Console.ReadLine());
结果=添加(firstNum,secondNum);
Console.WriteLine(,secondNum,firstNum,secondNum,结果你选择了数{0} {1}加上{2}等于{3}。);
到Console.ReadLine();
中断;
案S:
Console.Write(你选择了减法请选择您的第一个数字:);
firstNum = int.Parse(到Console.ReadLine());
Console.Write(你选择了数{0}请选择第二个数字:firstNum);
secondNum = int.Parse(到Console.ReadLine());
结果=子(firstNum,secondNum);
Console.WriteLine(你选择了数{0} {1}减去{2}等于{3}。secondNum,firstNum,secondNum,结果);
到Console.ReadLine();
中断;
案M:
Console.Write(你选择乘请选择您的第一个数字:);
firstNum = int.Parse(到Console.ReadLine());
Console.Write(你选择了数{0}请选择第二个数字:firstNum);
secondNum = int.Parse(到Console.ReadLine());
结果= MULT(firstNum,secondNum);
Console.WriteLine(,secondNum,firstNum,secondNum,结果你选择了数{0} {1}次{2}等于{3}。);
到Console.ReadLine();
中断;
案D:
Console.Write(你选择了科请选择您​​的第一个数字:);
firstNum = int.Parse(到Console.ReadLine());
Console.Write(你选择了数{0}请选择第二个数字:firstNum);
secondNum = int.Parse(到Console.ReadLine());
结果=股利(firstNum,secondNum);
Console.WriteLine(你选择了数{0} {1} {2}等于{3}分。secondNum,firstNum,secondNum,结果);
到Console.ReadLine();
中断;
默认:
Console.WriteLine(。我很抱歉{0}不是一个可用的选项,请重试,whichOp.ToUpper());
重试= TRUE;
中断;
}
}
}

静态INT添加(INT NUM1,NUM2 INT)
{
INT theAnswer;

theAnswer = NUM​​1 + NUM2;

返回theAnswer;
}

静态INT MULT(INT NUM1,NUM2 INT)
{
INT theAnswer;

theAnswer = NUM​​1 * NUM2;

返回theAnswer;
}

静态INT子(INT NUM1,NUM2 INT)
{
INT theAnswer;

theAnswer = NUM​​1 - NUM2;

返回theAnswer;
}

静态中期息(INT NUM1,NUM2 INT)
{
INT theAnswer;

如果(NUM2 == 0)
返回0;

theAnswer = NUM​​1 / NUM2;

返回theAnswer;
}


解决方案

(1)开关通常是更清洁的方式来表达这种分支。开关的主要限制是它只能在编译时间常数的值(const的变量,文字字符串,整数,枚举等)。在这种情况下,似乎不成为一个问题,因此,开关可能会是更清洁,稍短码的好选择。在性能敏感的代码(这当然不是),一个开关可以比一堆IFS的更快,因为该计划将测试值,并直接跳转到正确的情况下,而不是针对每个if条件测试,直至达到匹配。



(2)一个简单的方法来做到这将是一个循环来包装整个程序:

  VAR重试= TRUE; 
,而(重试)
重试= FALSE;
//你的程序
,否则{//或默认:如果你带开关
...
重试= TRUE去;
}
}


I am teaching myself C# and the current chapter challenge asked me to:

Take your last project and create additional methods that subtract, multiply or divide the two numbers passed into it. In the divide method, check that the second number is not 0, since dividing by 0 is an illegal mathematical concept. If the second number is a 0, just return back a 0.

Now I wrote the below which I believe satisfies all the criteria. I wasn't sure if an IF statement was the best choice but it did work. I also thought that a SWITCH would have also done the trick.

So first question, would IF or SWITCH have been better?

Second question. In the ELSE, I give a generic failure message if the user does not select one of the available options, but what I would like to do is if the ELSE is called (Not sure what the correct term is), I want the program to go back to the start and ask the user to try again and display the first Console.Writeline() asking to choose an Operator. I know this isn't part of the challenge, but it seems like a logical addition to the program and would like to know if this is possible without resorting to anything too complicated.

Thanks in advance!

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

EDIT: I took the suggestions of those here and rebuilt the program with SWITCH and WHILE. Someone also said that because a lot of the code is the same I should be able to reuse it. I like that idea and will look into how I can do that.

        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 is often a cleaner way to express this kind of branching. The major limitation of switch is that it only works on compile-time constant values (const variables, literal strings, ints, enums, etc.). In this case, that doesn't seem to be an issue, so switch would probably be a good choice for cleaner, slightly shorter code. In performance-sensitive code (which this is certainly not), a single switch can be faster than a bunch of ifs because the program will test the value and jump straight to the right case rather than testing against each if condition until reaching a match.

(2) an easy way to do this would be to wrap your entire program in a loop:

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

这篇关于返回程序的顶部再试一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆