C# - 我的列表中的项目不会显示 [英] C# - Items in my list wont show

查看:76
本文介绍了C# - 我的列表中的项目不会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我希望我的列表中的项目显示在控制台中...但我得到这个:



SimpleTextAdventure.Item

SimpleTextAdventure.Item

SimpleTextAdventure.Item





列表中有3项,所以我知道已添加的项目。但是不要把它们写成







火炬



已完成上述操作。为什么会这样?如果你想知道它是什么意思



SimpleTextAdventure是项目的名称,而.Item是其中一个类的名称。



请帮忙,我一整天都坚持这一点谢谢...



Basically I want the items in my list to show in the console... but instead i get this:

SimpleTextAdventure.Item
SimpleTextAdventure.Item
SimpleTextAdventure.Item


There are 3 items in the list, so i know the items have been added. But instead of writing them out like

Sword
Shield
Torch

Is has done the above instead. Why is this happening? If your wondering what it means

SimpleTextAdventure is the name of the project, and .Item is the name of one of the classes.

Please help, ive been stuck on this all day long thanks...

public class Inventory
    {
        // Attributes
        private static List<Item> _inventory = new List<Item>();

        // Constructors

        // Properties

        // Methods
        public static void AddItem(string item)
        {
            _inventory.Add(new Item(item));

        }

        public static void PrintItems()
        {
            foreach (Item itemName in _inventory)
            {
                Console.WriteLine(itemName);
            }
        }

        public static void RemoveItem(string itemName)
        {
            for (int i = 0; i < _inventory.Count; i++)
            {
                if (_inventory[i].ItemName == itemName)
                {
                    _inventory.Remove(_inventory[i]);
                    break;
                }
            }
        }

        public static bool HasItem(string itemName)
        {
            foreach (Item item in _inventory)
            {
                if (item.ItemName == itemName)
                {
                    return true;
                }
            }

            return false;
        }

        public static int CountItemsFromInventory(string inventory)
        {
            int count = 0;

            foreach (Item item in _inventory)
            {
                if (item.ItemName == inventory)
                {
                    count++;
                }
            }

            return count;
        }
    }
}













class Item
    {
        // Attributes
        string _itemName;

        // Constructors

        public Item(string itemName)
        {
            _itemName = ItemName;
        }

        // Properties
        public string ItemName { get { return _itemName; } }

        // Methods
        public void PrintOut(Inventory _inventory)
        {
            Console.WriteLine(_inventory);
        }
    }
}













public class RoomThree:Room
    {
        // Attributes
        public static string[] _availableItems = new string[] { "torch", "shield", "sword" };


        public static ChatLine[] _lines = new ChatLine[]
        {
           // new ChatLine("START", "Welcome to Room 3.. There are doors to the #DIRECTIONS#.", "move+?", GameEngine.Instance.PlayerMove),
            new ChatLine("START", "Welcome to Room 3. There is a table with weapons and a phone ringing on it. " +
                "I would suggest you answer it", "answer, answer phone", AnswerPhone),
            new ChatLine("TABLE", "There are items on the table", "look, look table", LookTable),
            new ChatLine("FOUND_ITEMS", "You looked at the table and there #AVAILABLE_ITEMS#.", "", PlayerSelectsItem),
            new ChatLine("MOVE_OR_PICK", "Move towards the North door, or pick another item, there #AVAILABLE_ITEMS#", "pick+?", PlayerSelectsItem),
            new ChatLine("MOVE_OR_PICK", "Move towards the North doorm or pick another item, there #AVAILABLE_ITEMS#", "move+?", GameEngine.Instance.PlayerMove),


            new ChatLine("", "", "pick+?, select+?, choose+?", PlayerSelectsItem),
            new ChatLine("", "", "drop+?", PlayerDropsItem),
            new ChatLine("", "", "move+?", GameEngine.Instance.PlayerMove)

        };

        // Constructors

        public RoomThree()
            : base("ROOM_THREE", _lines,  "START", _availableItems)
        {
        }

        // Methods


        private static string AnswerPhone(string word, object[] args)
        {
            Console.WriteLine();
            Console.WriteLine("Hello human. My name is Sauron. I am the leader of this race. You have became a problem for me," +
                " and for my race. But I see potential in you. I see a great warrior in you.. So I will offer you this deal...." +
                "If you can get to me, I will offer you a commander role leading my army against the demons. But it will be tough, you" +
                " will have to face very experienced fighting orcs and you will have to find your way to me.. But IF you SURVIVE." +
                " I will make you my second in command. Now there are weapons on the table in front of you, and an orc in the other room." +
                " Good luck. You will need it.");

            Console.WriteLine();

            return "TABLE";
        }

        private static string LookTable(string word, object[] args)
        {
            Console.WriteLine();

            return "FOUND_ITEMS";
        }

        public static string PlayerSelectsItem(string word, object[] args)
        {
            GameEngine.Instance.RemoveItemFromRoom(word);
            Inventory.AddItem(word);
            Inventory.PrintItems();
            Console.WriteLine();
            return "MOVE_OR_PICK";  // Don't change action
        }

        public static string PlayerDropsItem(string word, object[] args)
        {
            if (Inventory.HasItem(word))
            {
                Inventory.RemoveItem(word);
                GameEngine.Instance.DropItemInRoom(word);
                Console.WriteLine("Item has been dropped");
            }
            else
            {
                Console.WriteLine("You don't have that item");
            }

            return "";  // Don't change action
        }
    }

推荐答案

无法正确理解您的查询但是我理解您需要在控制台上显示列表项。如果您的课程如下:



Not able to understand your query properly but as i am understanding you need to display list item on console .If your class like below :

class Items
    {
        List<string> lst = new List<string>();
        

       
        public IList GetList()
        {
            lst.Add("Sword");
            lst.Add("Shield");
            lst.Add("Torch");
            return lst;
        }

    }





然后您可以使用以下代码显示列表值:





then you can display list value by using such code :

static void Main(string[] args)
        {

            Items _items = new Items();
            IList lst = _items.GetList();

            foreach(string l in lst)
            {
                Console.WriteLine(l);
              
            }
            Console.ReadKey();
          }


与之前的其他问题(几乎相同的代码)一样,它是同一类型的问题。你绑定了对象Item(名称的选择不好)而不是Item的ItemName属性。



你需要像这样更改PrintItems。



Like the other question earlier (almost Identical code) it is the same type of problem. You are binding the object Item (poor choice for a name) and not the ItemName property of the Item.

you NEED to change the PrintItems like this.

public static void PrintItems()
{
    foreach (Item itemName in _inventory)
    {
        Console.WriteLine(itemName.ItemName);
    }
}


我解决了这些家伙:)...基本上,它是愚蠢的东西....我基本上不得不把一个字母从大写改为小写...





而不是... itemName ...我有它的ItemName。 ...







I solved it guys :)... basically, it was something soo stupid.... I basically had to change a letter from uppercase to lowercase...


Instead of ...itemName... i had it ItemName....



public static void PrintItems()
        {
            foreach (Item itemName in _inventory)
            {
                Console.WriteLine(itemName.itemName);
            }
        }
















public Item(string itemName)
{
    _itemName = itemName;
}

// Properties
public string itemName { get { return _itemName; } }


这篇关于C# - 我的列表中的项目不会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆