C#添加方法并检查是否已添加数字

时间:2012-07-11 10:09:07

标签: c# methods int

我正在玩一个小程序。需要确保它检查是否数字,如果不是循环,直到它们是每个输入上的数字并创建主方法和计算器方法任何帮助?

代码在这里//////////////////////////////////////////// /////////

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




        ////enter first number ////
        Console.Write("Please enter a number: ");
        text1 = Console.ReadLine();

        // if number not integer then fail ////
        bool res = int.TryParse(text1, out num1);
        if (!res)
        {
            Console.WriteLine(" FAIL");
            ////enter first number ////
            Console.Write("Please enter a number: ");
            text1 = Console.ReadLine();
        }
        else
        {

        }



        //// enter operand ////
        Console.Write("Please enter an operand (+, -, /, *): ");
        operand = Console.ReadLine();



        // enter second number //
        Console.Write("Please enter the second number: ");
        text1 = Console.ReadLine();

        // if number not integer then fail //
        bool eff = int.TryParse(text1, out num2);
        if (!eff)
            do
            {
                Console.WriteLine(" FAIL");
                // enter second number //
                Console.Write("Please enter the second number: ");
                text1 = Console.ReadLine();
            }
            while (eff == true);
        {

        }
        // converts number to integer ///


        // makes operand answers from each number ////
        switch (operand)
        {
            case "-":
                answer = num1 - num2;
                break;
            case "+":
                answer = num1 + num2;
                break;
            case "/":
                answer = num1 / num2;
                break;
            case "*":
                answer = num1 * num2;
                break;
            default:
                answer = 0;
                break;
        }

        /// converts numbers to string using operand and writes final line ///
        Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " =                   
        "+ answer.ToString());
        Console.ReadLine();
        }

        }


        }

        }

        /// converts numbers to string using operand and writes final line ///
        Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = 
        " + answer.ToString());
        Console.ReadLine();

2 个答案:

答案 0 :(得分:0)

如果这就是您所需要的并且您正在使用控制台应用程序,则可以使用:

        int num1;
        int num2;
        string operand = string.Empty;
        float answer;
        string text1;

        ////enter first number ////
        Console.Write("Please enter a number: ");
        text1 = Console.ReadLine();

        // if number not integer then fail ////
        bool res = int.TryParse(text1, out num1);
        while (!res)
        {
            Console.WriteLine(" FAIL");
            ////enter first number ////
            Console.Write("Please enter a number: ");
            text1 = Console.ReadLine();
            res = int.TryParse(text1, out num1);
        }

        //// enter operand ////
        while (operand == string.Empty || operand.Length > 1 || !(new char[] { '+', '-', '*', '/' }).Contains(char.Parse(operand)))
        {
            Console.Write("Please enter an operand (+, -, /, *): ");
            operand = Console.ReadLine();
        }


        // enter second number //
        Console.Write("Please enter the second number: ");
        text1 = Console.ReadLine();

        // if number not integer then fail //
        bool eff = int.TryParse(text1, out num2);
        while (!eff)
        {
            Console.WriteLine(" FAIL");
            // enter second number //
            Console.Write("Please enter the second number: ");
            text1 = Console.ReadLine();
            eff = int.TryParse(text1, out num2);

        }
        // converts number to integer ///


        // makes operand answers from each number ////
        switch (operand)
        {
            case "-":
                answer = num1 - num2;
                break;
            case "+":
                answer = num1 + num2;
                break;
            case "/":
                if (num2 == 0)
                {
                    Console.WriteLine("Divide By Zero Error");
                    return;
                }
                answer = num1 / num2;
                break;
            case "*":
                answer = num1 * num2;
                break;
            default:
                answer = 0;
                break;
        }

        /// converts numbers to string using operand and writes final line ///
        Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " =  "+ answer.ToString());
        Console.ReadLine();

为什么需要为这么简单的事情创建一个单独的方法。我发现这个问题太简单了,所以只是问这是否是必需的?你本可以挣扎一些,自己写一下。这没有什么微不足道的。我只是假设你是编程新手。

答案 1 :(得分:0)

问题似乎是处理“eff”和“res”。如果用户在第一次询问时键入非整数值,则第二次他们回答什么并不重要,因为num1和num2没有填充该值。修复它,代码似乎有效。

由于输入值似乎做同样的事情,但提示略有不同,你应该把它移到一个单独的函数中,如下所示:

    static int GetNumberFromUser(string order)
    {
        string userText = String.Empty;
        int result;

        Console.Write("Please enter {0} number: ", order);
        userText = Console.ReadLine();

        while (!int.TryParse(userText, out result))
        {
            Console.WriteLine("FAILED");
            Console.Write("Please enter {0} number: ", order);
            userText = Console.ReadLine();
        }
        return result;
    }

然后你可以通过调用

来调用它
    num1 = GetNumberFromUser("first");
    num2 = GetNumberFromUser("second");

此功能负责转换为数字并一直询问,直到用户键入有效值。

只需复制“计算器”方法并将您所拥有的开关粘贴到单独的方法中:

    static float Calculator(int num1, string operand, int num2)
    {
        switch (operand)
        {
            case "-":
                return num1 - num2;
            case "+":
                return num1 + num2;
            case "/":
                return num1 / num2;
            case "*":
                return num1 * num2;
            default:
                return 0;
        }
    }

使用

调用
        answer = Calculator(num1, operand, num2);

虽然我在这里,但结果很难读懂,我会选择这样的东西

    Console.WriteLine("{0} {1} {2} = {3}", num1, operand, num2, answer);