返回到程序顶部重试

2023-11-30

我正在自学 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) switch 通常是表达这种分支的更简洁的方式。 switch 的主要限制是它仅适用于编译时常量值(const 变量、文字字符串、整数、枚举等)。在这种情况下,这似乎不是问题,因此 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;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

返回到程序顶部重试 的相关文章

  • C++:如何检测向量中的重复项并打印一份副本?

    我是 C 新手 我想知道如何在向量中找到重复的字符串并打印出该字符串的一个副本 例如 如果我有它会打印出cat dog bird 我已经对我的向量进行了排序 并使用adjacent find函数并迭代该向量 因为我必须查找是否有任何单词重复
  • 如何使用 VS2022 中的新控制台应用程序模板访问命令行参数

    我想知道如何访问命令行参数 因为这是在Program cs通过 Visual Studio 2022 中控制台应用程序的新模板创建文件 See https aka ms new console template for more infor
  • 在哪里使用 EF6 订阅 ObjectMaterialized?

    我正在尝试将我的上下文订阅到以下 OnjectMaterialized 事件this https stackoverflow com a 3756842 2835713 像这样 IObjectContextAdapter this Obje
  • MVC 中的 Blazor:组件被渲染,但 @onclick 不起作用。连接问题

    我正在尝试在 net core 3 MVC 项目中使用 Blazor 我使用了一些教程来做到这一点 例如https fizzylogic nl 2019 08 18 integrating blazor in an existing asp
  • 如何自定义 ASP.Net Core 模型绑定错误?

    我只想从我的 Web API Asp net Core 2 1 返回标准化的错误响应 但我似乎不知道如何处理模型绑定错误 该项目刚刚从 ASP NET Core Web 应用程序 gt API 模板创建 我有一个简单的操作定义为 Route
  • std::async 参数的生命周期是多少?

    看来函数的参数是通过std async分享未来的生活 include
  • 使用 pthread_cond_signal 优雅地终止线程被证明是有问题的

    我需要发射一堆线程 并希望优雅地将它们拉下来 我正在尝试使用pthread cond signal pthread cond wait实现这一目标 但遇到了问题 这是我的代码 首先是thread main static void thrma
  • 我可以将特定警告视为错误吗?

    以下是我有时在学生代码中看到的模式的简化版本 bool foobar int a int b if a lt b return true 当然 真正的代码要复杂得多 Visual Studio 报告警告 C4715 并非所有控制路径都会返回
  • C++ Linux GCC 应用程序中的 GUID

    我有很多服务器运行这个 Linux 应用程序 我希望他们能够生成一个碰撞概率较低的 GUID 我确信我可以从 dev urandom 中提取 128 个字节 这可能没问题 但是有没有一种简单易用的方法来生成与 Win32 更等效的 GUID
  • 本地主机和 request.Url.Authority

    我的应用程序通过 URL 中的公司标识符分隔用户 company1 app com company2 app com 我正在本地 PC 上进行测试 请求如下 company1 localhost com 但是 我的 request Url
  • C++ 虚拟关键字与重写函数

    我正在学习c 并且正在学习virtual关键字 我在互联网上搜索试图理解它但无济于事 我进入编辑器并做了以下实验 期望它打印两次基本消息 因为我的印象是需要 virtual 关键字来覆盖函数 然而 它打印出了两条不同的消息 有人可以向我解释
  • 使用 C# 和 .NET Core 在 AWS Cognito 用户池中进行用户管理

    如何使用 C 和 NET Core 3 x 管理 AWS Cognito 用户池中的用户 在文档中找不到有关它的任何内容 Attilio Gelosa 的原创文章 我写这篇文章是希望对其他人有帮助 我必须阅读一页又一页的文档 并从 AWS
  • 在 OpenGL 中使用不同的着色器程序?

    我必须在 OpenGL 中针对不同的对象使用两个不同的着色器程序 我发现我必须使用glUseProgram 在不同的着色器程序之间切换 但对此没有太多信息 鉴于我有两个用于不同对象的不同着色器程序 如何为每个着色器程序生成和绑定 VAO 和
  • 如何在 C 预处理器中可靠地检测 Mac OS X、iOS、Linux、Windows? [复制]

    这个问题在这里已经有答案了 如果有一些跨平台 C C 代码需要在 Mac OS X iOS Linux Windows 上编译 我如何在预处理器过程中可靠地检测到它们 大多数编译器都使用预定义的宏 您可以找到列表here http sour
  • 如何明智地解释这个编译器警告?

    当我执行这段代码时question https stackoverflow com a 51056490 2411320 我收到这个警告 warning format d expects argument of type int but a
  • std::iota 的 iota 代表什么?

    我假设 i 是增量 a 是分配 但我无法弄清楚或找到答案 而且 它看起来与非标准非常相似itoa我认为这很令人困惑 C iota is not an acronym or an initialism It is the word iota
  • 如何正确地将十六进制转义添加到字符串文字中?

    当你有C语言的字符串时 你可以在里面直接添加十六进制代码 char str abcde a b c d e 0x00 char str2 abc x12 x34 a b c 0x12 0x34 0x00 这两个示例在内存中都有 6 个字节
  • 如何在您的网站中连接两个人

    有一款名为 Verbosity 的游戏 这是一款有目的的游戏 位于此链接上www gwap com 在游戏中 他们随机连接两个玩家互相玩 游戏是玩家1应该向他的搭档 玩家2 描述一个单词 而玩家2应该猜测这个单词 我正在尝试建立一个网站来执
  • 频繁插入已排序的集合

    我已经对集合 列表 进行了排序 并且我需要始终保持其排序 我目前在我的集合上使用 List BinarySearch 然后在正确的位置插入元素 我也尝试过在每次插入后对列表进行排序 但性能不可接受 有没有一种解决方案可以提供更好的性能 也许
  • 应用非限定名称查找而不是依赖于参数的名称查找

    考虑标准 sec 3 4 1 3 中的一个示例 typedef int f namespace N struct A friend void f A operator int void g A a int i f a f is the ty

随机推荐