如何使单独的功能在C#中打印输出?

时间:2018-07-26 10:35:28

标签: c# function c#-7.0

我正在学习C#中的函数,并决定制作一个计算器。这是我编写的代码:-

 using System;

    namespace Second_Project
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Entet any two numbers to find out its sum, product, difference, division & remainder\n");
                Console.WriteLine("Enter first number:-");
                string input1 = Console.ReadLine();
                Console.WriteLine("Enter second number:-");
                string input2 = Console.ReadLine();
                int num1 = int.Parse(input1);
                int num2 = int.Parse(input2);
                Calculation(num1,num2);
                Console.Read();
            }

            private static void Calculation(float n, float m)
            {
                float sum, sub, mul, div, mod;
                sum = n + m;
                sub = n - m;
                mul = n * m;
                div = n / m;
                mod = n % m;
                Console.WriteLine("Sum of " + n + " & " + m + "=" + sum);
                Console.WriteLine("Differnce of " + n + " & " + m + "=" + sub);
                Console.WriteLine("Divission of " + n + " & " + m + "=" + div);
                Console.WriteLine("Multiplication of " + n + " & " + m + "=" + mul);
                Console.WriteLine("Remainder of " + n + " & " + m + "=" + mod);
            }

        }
    }

但是,我想为计算和打印创建不同的功能,并在尝试这样做时出现错误:-

private static void Calculation(float n, float m)
    {
           float sum, sub, mul, div, mod;
           sum = n + m;
           sub = n - m;
           mul = n * m;
           div = n / m;
           mod = n % m;
     }    

private static void print()
{
           Console.WriteLine("Sum of " + n + " & " + m + "=" + sum);
           Console.WriteLine("Differnce of " + n + " & " + m + "=" + sub);
           Console.WriteLine("Divission of " + n + " & " + m + "=" + div);
           Console.WriteLine("Multiplication of " + n + " & " + m + "=" + mul);
           Console.WriteLine("Remainder of " + n + " & " + m + "=" + mod);
}

对不起,我知道这是一个新手问题,但我仍在学习中。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要按如下方法创建新方法。

private static void PrintData(float n, float m, float sum, float sub, float mul , float div, float mod)
        {

            Console.WriteLine("Sum of " + n + " & " + m + "=" + sum);
            Console.WriteLine("Differnce of " + n + " & " + m + "=" + sub);
            Console.WriteLine("Divission of " + n + " & " + m + "=" + div);
            Console.WriteLine("Multiplication of " + n + " & " + m + "=" + mul);
            Console.WriteLine("Remainder of " + n + " & " + m + "=" + mod);
        }

,然后从Calculation方法中调用此方法,如下所示。

private static void Calculation(float n, float m)
        {
            float sum, sub, mul, div, mod;
            sum = n + m;
            sub = n - m;
            mul = n * m;
            div = n / m;
            mod = n % m;
            PrintData(n, m,sum, sub, mul, div, mod);
        }