【问题标题】:Using Lists as method arguments [closed]使用列表作为方法参数 [关闭]
【发布时间】:2017-04-29 01:24:00
【问题描述】:

我正在制作一个基本的城市建设者游戏(在控制台中)。我有一个方法(DrawMap)的问题。我无法将列表用作该方法的输入参数。我收到一大堆错误,所以这里是代码。

编辑:现在可以使用了,谢谢 kmatyaszek。

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

namespace City
{
    public class map
    {
        public int m { get; set; } //Map size
        public List<int> info { get; set; }
        public List<int> fire { get; set; }
        public List<int> police { get; set; }
        public List<int> education { get; set; }
        public List<int> health { get; set; }
        public List<int> cursor { get; set; }
    }
    class Program
    {

        static void Main(string[] args)
        {
            map map1 = new map();
            map1.m = 256;

            map1.info = new List<int>();
            for (int i = 0; i < map1.m; i++)
            {
                map1.info.Add(0);
            }

            map1.fire = new List<int>();
            for (int i = 0; i < map1.m; i++)
            {
                map1.fire.Add(0);
            }
            map1.police = new List<int>();
            for (int i = 0; i < map1.m; i++)
            {
                map1.police.Add(0);
            }
            map1.education = new List<int>();
            for (int i = 0; i < map1.m; i++)
            {
                map1.education.Add(0);
            }
            map1.health = new List<int>();
            for (int i = 0; i < map1.m; i++)
            {
                map1.health.Add(0);
            }
            map1.cursor = new List<int>() { 0, 0 };

            DrawMap(map1.info, map1.cursor);
        }

        static void DrawMap(List<int> map1.info, List<int> map1.cursor)
        {
        int j = 0;
        int k = 0;
            for (int k = 0; k < Math.Sqrt(map1.m); k++)
            {
                Console.SetCursorPosition(map1.cursor[j], map1.cursor[k]);       

            for (int j = 0; j < Math.Sqrt(map1.m); j++)
            {
            Console.SetCursorPosition(map1.cursor[j], map1.cursor[k]);  
            Console.Write("A");  
            }
            }
        }
    }
}

【问题讨论】:

  • 您愿意分享这些错误吗?是不是跟DrawMaps方法的参数赔率有关系?
  • 为什么你有像 map1.info 这样的变量名?这是c#中的非法变量名
  • @DanHunex 它是 map1 对象的一部分。它现在似乎工作得很好。
  • 你不能这样声明变量。但是当您调用该方法时,您可以像这样传递它
  • @DanHunex 好吧,它似乎完美无缺。我应该怎么做呢?

标签: c# list methods arguments


【解决方案1】:

您应该阅读 C# 方法 (https://msdn.microsoft.com/en-us/library/ms173114.aspx)。

我认为DrawMap 方法应该采用map 对象:

...
map1.health = new List<int>();
        for (int i = 0; i < map1.m; i++)
        {
            map1.health.Add(0);
        }
        map1.cursor = new List<int>() { 0, 0 };

        DrawMap(map1);
    }

    static void DrawMap(map map1)
    {
        int j = 0;
        int k = 0;
        for (k = 0; k < Math.Sqrt(map1.m); k++)
        {
            Console.SetCursorPosition(map1.cursor[j], map1.cursor[k]);

            for (j = 0; j < Math.Sqrt(map1.m); j++)
            {
                Console.SetCursorPosition(map1.cursor[j], map1.cursor[k]);
                Console.Write("A");
            }
        }
    }
...

DrawMap 中,您在同一范围内声明了两个本地变量(j 和 k)。你不能那样做。

您可以在此处阅读有关局部变量和范围的信息: https://blogs.msdn.microsoft.com/samng/2007/11/09/local-variable-scoping-in-c/

【讨论】:

    【解决方案2】:

    我不知道从哪里开始。让我们从DrawMap 方法的参数开始。 C# 不允许在变量名中使用 .。当你声明一个方法的签名时,你只写参数的名字。不要试图引用程序中的现有变量。只需选择一个名称。当您在方法调用中传递它们时,编译器会知道您的意思的快速列表:

    DrawMap(map1.info, map.cursor);
    

    在你给你的方法的参数起正确的名字之后:

    static void DrawMap(List<int> info,  List<int> cursor)
    

    您可以在方法范围内使用名称。

    第二件事是你在方法中声明你的索引变量两次。查看您的 for 循环声明。那里有int k=0; k&lt;...,这意味着声明了一个同名的新变量。只需删除循环上方的两个变量即可。

    【讨论】:

      猜你喜欢
      • 2021-08-14
      • 1970-01-01
      • 2016-04-28
      • 2020-01-06
      • 1970-01-01
      • 2017-03-08
      • 2020-07-02
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多