【问题标题】:Repeat a function if the output is undefined如果输出未定义,则重复一个函数
【发布时间】:2016-09-28 21:46:06
【问题描述】:

好的,我是 C# 新手,我只想用控制台创建一个简单的计算器。我的问题是,当用户选择除零或除以零时,如何让用户为 num1 和 num2 选择不同的值,以防止程序由于未定义的结果而崩溃。我想我可以使用一个while循环来检查并查看答案是否为int,如果不是则重复整个程序。如何检查答案以验证结果是否为 int?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
         public static void Main(string[] args)
    {

        int num1;
        int num2;
        string operand;
        float answer;

        while ()
        {

            Console.WriteLine("Please enter the first number.");
        num1 = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Please enter which operator you would like to use(+,-,*,/)");
        operand = Console.ReadLine();

        Console.WriteLine("Please enter the second number.");
        num2 = Convert.ToInt32(Console.ReadLine());

            switch (operand)
            {
                case "+":
                    answer = num1 + num2;
                    Console.WriteLine(num1 + " + " + num2 + " = " + answer);
                    Console.ReadKey();
                    break;
                case "-":
                    answer = num1 - num2;
                    Console.WriteLine(num1 + " - " + num2 + " = " + answer);
                    Console.ReadKey();
                    break;
                case "*":
                    answer = num1 * num2;
                    Console.WriteLine(num1 + " * " + num2 + " = " + answer);
                    Console.ReadKey();
                    break;
                case "/":
                    if (num2 == 0)
                    {

                        Console.WriteLine("You cannot devide by zero, please select a different number.");
                        num2 = Convert.ToInt32(Console.ReadLine());
                    }
                    else if (num1 == 0)
                    {
                        Console.WriteLine("You cannot devide zero, please select a different number.");
                        num1 = Convert.ToInt32(Console.ReadLine());
                    }
                    else
                    {
                        answer = num1 / num2;
                        Console.WriteLine(num1 + " / " + num2 + " = " + answer);
                        Console.ReadKey();

                    }
                    break;
                }

            }
        }
    }
}

【问题讨论】:

  • 第一:始终使用相关语言进行标记。第二:示例应该最少,重点不在于您试图完成的目标,而是您在尝试完成它时遇到的具体问题,并且只包含足够的代码来重现该问题;见stackoverflow.com/help/mcve

标签: c# loops while-loop int calculator


【解决方案1】:

我认为cleanset解决方案是提取一些方法(选择代码并用鼠标右键单击->重构->extractMethod)。 其中一个功能应该是“getSecondNumber” 在该函数中,您以“输入第二个数字或单击“q”退出。

如果 Int.TryParse 结果为 false,或者 number==0 再次调用您的函数,则结束(您将到达“输入第二个数字...”的起始位置 祝你好运

【讨论】:

    【解决方案2】:

    一个可能的解决方案是使用while,见下文:

                case "/":
                    while (num2 == 0)
                    {
    
                        Console.WriteLine("You cannot devide by zero, please select a different number.");
                        num2 = Convert.ToInt32(Console.ReadLine());
                    }
    
                    answer = num1 / num2;
                    Console.WriteLine(num1 + " / " + num2 + " = " + answer);
                    Console.ReadKey();
    
                    break;
    

    但是,您应该考虑更改为使用int.TryParse 方法(https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx),就好像用户键入的不是数字一样,Convert.ToInt32 会抛出一个FormatExceptionhttps://msdn.microsoft.com/en-us/library/sf1aw27b%28v=vs.110%29.aspx) .

    【讨论】:

      【解决方案3】:

      您实际上只需要检查第二个值 (num2)。您想在阅读后立即进行评估。你如何处理它取决于你。您可以无限循环一段时间,但您还需要验证(与任何这些输入一样),并在用户无法理解输入方向时为他们提供退出选项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-07
        • 1970-01-01
        • 2017-03-19
        • 1970-01-01
        • 2023-02-05
        • 2021-10-16
        • 2017-01-11
        相关资源
        最近更新 更多