2016-12-13 71 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<...這意味着一個新的變量具有相同的名稱被宣佈。只需刪除循環上方的兩個變量即可。