我一直在努力的代碼和什麼地方出了錯:http://ideone.com/cvLRLgC#基本OOP - 製作類的詞典與構造
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Minion
{
public static int manaCost;
public static int attack;
public static int health;
public static string cardText;
public Minion(int mana, int atk, int h, string txt)
{
manaCost = mana;
attack = atk;
health = h;
cardText = txt;
}
public void displayStats(string name)
{
Console.WriteLine(name + "\nMana Cost: " + manaCost + "\nAttack: " + attack + "\nHealth: " + health + "\n" + cardText + "\n");
}
}
class Program
{
static void Main(string[] args)
{
List<string> indexList = new List<string>();
Dictionary<string, Minion> minionList = new Dictionary<string, Minion>();
//buffer so I start at 1 and not 0
indexList.Add("MissingNo");
//make a Wolfrider card
indexList.Add("Wolfrider");
Minion Wolfrider = new Minion(3, 3, 1, "Charge");
minionList.Add(indexList[1], Wolfrider);
//make a Goldshire Footman card
indexList.Add("Goldshire Footman");
Minion GoldshireFootman = new Minion(1, 1, 2, "Taunt");
minionList.Add(indexList[2], GoldshireFootman);
//look through all my cards
for (int i = 1; i < indexList.Count(); i++)
minionList[indexList[i]].displayStats(indexList[i]);
Console.ReadLine();
}
}
}
我一直在努力自學C#但是這已經絆倒了我。我想創建一個接受字符串的Dictionary然後返回一個Minion(新類)。
Minion接受四個參數,所以我必須專門用一行代碼來製作一個新的Minion,然後再添加到Dictionary中。
但是,當我經歷所有我有的僕從時,出於某種原因,第一個將我退回其他僕從的屬性。
Wolfrider
Mana Cost: 1
Attack: 1
Health: 2
Taunt
Goldshire Footman
Mana Cost: 1
Attack: 1
Health: 2
Taunt
該列表工作正常,因爲名稱是正確的...但是Wolfrider具有Goldshire Footman的屬性。
有沒有更高效/最優化的方法來做到這一點?如果不是,我做錯了什麼?
它的工作原理!非常感謝! –