输出到控制台窗口

时间:2016-04-11 23:41:56

标签: c# console-application

琐碎的问题,但我如何将结果输出到控制台窗口。不知道如何将该数据类型传递给main。我的代码如下:

namespace Node
    {
        class Program
        {
            public static int appLayer(int humData)
            {
                int tempData1 = 55;
                int tempData2 = 60;
                int tempData3 = 58;
                int tempData4 = 58;
                int [] tempData = new int[] {tempData1, tempData2, tempData3, tempData4};

                Dictionary<int, int> count = new Dictionary<int, int>();
                foreach (int a in tempData)
                {
                    if (count.ContainsKey(a))
                        count[a] = count[a] + 1;
                    else
                        count[a] = 1;
                }
                int result = int.MinValue;
                int max = int.MaxValue;
                foreach (int key in count.Keys)
                {
                    if (count[key] > max)
                    {
                        max = count[key];
                        result = key;
                    }
                }
                return result;
            }
            static void Main(string[] args)
            {
                Console.WriteLine("The mode is: " + result);
            }
        }

谢谢

2 个答案:

答案 0 :(得分:1)

static void Main(string[] args)
{    
   int result = appLayer();
   Console.WriteLine("The mode is: " + result);
}

OR

static void Main(string[] args)
{    
   Console.WriteLine("The mode is: " + appLayer());
}

appLayer的原型应该是

public static int appLayer()

因为未使用humData参数。

答案 1 :(得分:1)

您必须在main中调用方法并添加Console.Read()或Console.ReadLine()

        static void Main(string[] args)
        {
            var result = appLayer(0); // 0 is the parameter value for humData which is not used so you can remove it...
            Console.WriteLine("The mode is: " + result);
            Console.ReadLine(); // Add this to the end to have a pause on the console app
        }          
相关问题