【问题标题】:Display array using method and case显示数组使用方法及案例
【发布时间】:2014-03-05 05:04:37
【问题描述】:

您好,我写了一个案例,它应该显示我的方法中的数组,但我写的方法不会将值返回到我的案例,而是给我一个蓝色错误代码。我希望能够选择我的案例并让它显示我的数组。如果你能帮助我,我非常感谢。

我相信这些都是属于这个案例的所有代码。再次感谢。

            int selection;
            do
            {
            int[] A = { -2, 5, -1, 9, -6, 23, 67, 1, -8, 7, -3, 90 };

            DisplayMenu();

            Console.Write("Enter your selection");
            int.TryParse(Console.ReadLine(),out selection);

            switch(selection)
            {
            case 6:
                    ShowArray(A); //<<<<<display array here
                    break;
            }
            Console.ReadKey();



            static void DisplayMenu()
            {
            Console.WriteLine("t6");
            }



             static int ShowArray(int[] array)//<<<<here return value top
            {
            Console.WriteLine("\n=============\n");
            for (int i = 0; i < array.Length; i++)
            {
            Console.Write("{0}", array[i]);
            }
            Console.WriteLine("\n=============\n");
            }

【问题讨论】:

  • 这里的静态是什么意思 static int ShowArray(int[] array) ?并且您没有从此函数返回 anu "Int"

标签: c# arrays methods case


【解决方案1】:

假设您只想将数组显示到控制台,而不是实际从该方法返回值,请从以下更改签名:

static int ShowArray(int[] array)

到这里:

static void ShowArray(int[] array)
  • int 的返回类型意味着您将在方法退出之前返回一个整数。
  • void 的返回类型意味着您不会返回任何东西,这似乎是您想要的。

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2014-05-09
    • 1970-01-01
    相关资源
    最近更新 更多