2016-12-13 83 views
-3

我正在制作一个基本的城市建设者游戏(在控制台中)。我遇到了一个方法问题(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"); 
      } 
      } 
     } 
    } 
} 
+1

你介意分享这一堆错误吗?它与'DrawMaps'方法的参数可能性有什么关系? –

+1

为什么你有像map1.info这样的变量名?它是一个非法的变量名称在c# –

+0

@DanHunex它是map1对象的一部分。现在它似乎工作得很好。 – pippu

回答

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/

0

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

DrawMap(map1.info, map.cursor); 

你给你的方法的参数是这样的专有名词之后:

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

你可以使用方法范围内的名称。

第二件事是你在方法中声明你的索引变量两次。看看你的for-loop声明。你有int k=0; k<...这意味着一个新的变量具有相同的名称被宣布。只需删除循环上方的两个变量即可。