无法获取列表打印.. [英] Can't get list to print..

查看:39
本文介绍了无法获取列表打印..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,这是一个单一的项目...但是我整天都被困在它上面几个小时...



我想要的只是控制台打印出列表中的项目而不是我得到这个。





SimpleTextAdventure.Inventory



SimpleTextAdventure是项目本身的名称,库存是其中一个类的名称。



这里是所有类的代码。



 命名空间 SimpleTextAdventure 
{
< span class =code-keyword> public class PlayerItemInventory
{
/ / 属性
private static 列表<库存> _inventory = new 列表< Inventory>();

// 构造函数

// 属性

// 方法
public static void AddItem( string item)
{
_inventory.Add( new 库存(item));

}

public static void PrintItems()
{
foreach (库存商品名称 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 (库存商品 _inventory)
{
if (item.ItemName == itemName)
{
return true ;
}
}

返回 false ;
}

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

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

return count;
}
}
}

























  class 库存
{
// 属性
string _itemName;

// 构造函数

public 广告资源(字符串 itemName)
{
_itemName = ItemName;
}

// 属性
public string ItemName { get { return _itemName; }

// 方法


}
}



















  public   class  RoomThree:Room 
{
// 属性
public static string [] _availableItems = new string [] { torch shield sword };


public static ChatLine [] _lines = new ChatLine []
{
// 新的ChatLine(开始,欢迎来到3号房间。#DIRECTIONS#。,move +?,GameEngine.Instance.PlayerMove),
new ChatLine( START 欢迎来到3号房间。桌子上有武器和电话铃声。 +
我建议你回答 < span class =code-string> answer,answer phone,AnswerPhone),
new ChatLine( TABLE look,look table,LookTable),
new ChatLine( FOUND_ITEMS 您查看了表格,其中包含#AVAILABLE_ITEMS#。 ,PlayerSelectsItem),
new ChatLine( MOVE_OR_PICK 向北门移动,或选择另一个项目,#AVAILABLE_ITEMS# pick +?,PlayerSelectsItem),
new ChatLine( MOVE_OR_PICK,< span class =code-string> 向北方doorm移动或选择另一个项目,#AVAILABLE_ITEMS# move +?,GameEngine.Instance.PlayerMove),


new ChatLine( 选择+?,选择+ ?,选择+?,PlayerSelectsItem),
ChatLine( drop +? ,PlayerDropsItem),
new ChatLine( move +?,GameEngine.Instance.PlayerMove)

};

// 构造函数

public RoomThree()
base ROOM_THREE,_ lines, START,_ availabilityItems)
{
}

// 方法


private static string AnswerPhone( string word, object [] args)
{
Console.WriteLine();
Console.WriteLine( Hello human。我的名字是Sauron。我是这场比赛的领导者。你已成为我的问题, +
以及我的种族。但我看到了你看到了一个伟大的战士。所以我会给你这个交易.... +
< span class =code-string>如果你能找到我,我将为你提供一个指挥官角色,带领我的军队对抗恶魔。但这将是艰难的,你 +
将面对非常有经验的战斗兽人,你必须找到我的方式..但如果你生存。 +
我会让你成为我的第二把手。现在桌子前面有武器你和一个兽人我在另一个房间。 +
祝你好运。你需要它。);

Console.WriteLine();

return < span class =code-string> TABLE;
}

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

return FOUND_ITEMS;
}

public static string PlayerSelectsItem( string word, object [] args)
{
GameEngine.Instance.Remo veItemFromRoom(字);
PlayerItemInventory.AddItem(word);
PlayerItemInventory.PrintItems();
Console.WriteLine();
return MOVE_OR_PICK ; // 不要改变行动
}

public static string PlayerDropsItem( string word, object [] args)
{
if (PlayerItemInventory.HasItem(word))
{
PlayerItemInventory.RemoveItem(word);
GameEngine.Instance.DropItemInRoom(word);
Console.WriteLine( 项目已被放入房间);
}
else
{
Console.WriteLine( 您没有该项);
}

return ; // 不要更改操作
}
}
}











要扩展而不是像它应该打印列表项。它打印出namespace.class名称,在本例中是SimpleTextAdventure.Inventory ...









帮助!!!!!

解决方案

您正在遍历库存项目的_inventory列表。您正在调用每个'itemName',但它不是项目名称。所以每个itemName实际上都是一个Inventory对象,所以你得到的对象的'.ToString()'不是你想要的ItemName值。



在下面进行更改。



  public   static   void  PrintItems()
{
foreach (库存项目名称 _inventory)
{
Console.WriteLine(itemName。 ItemName );
}
}


您必须提供要打印的成员名称。在循环中,您命名为itemName的是库存类的实例。您选择的名称具有误导性。





  public  静态  void  PrintItems()
{
foreach (库存商品名 _inventory中)
{
Console.WriteLine(itemName.ItemName);
}
}







另一种方式(当你没有改变循环时打印到控制台)是覆盖库存类中的ToString()方法



  public  覆盖 字符串 ToString()


Basically, this is a uni project... But ive been stuck on it for hours all day long...

All i want is the console to print out a the items in the list instead i get this.


SimpleTextAdventure.Inventory

SimpleTextAdventure is the name of the project itself and the inventory is the name of one of the classes.

here is the code for all classes.

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

        // Constructors

        // Properties

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

        }

        public static void PrintItems()
        {
            foreach (Inventory 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 (Inventory item in _inventory)
            {
                if (item.ItemName == itemName)
                {
                    return true;
                }
            }

            return false;
        }

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

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

            return count;
        }
    }
}













class Inventory
    {
        // Attributes
        string _itemName;

        // Constructors

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

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

        // Methods


    }
}










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);
            PlayerItemInventory.AddItem(word);
            PlayerItemInventory.PrintItems();
            Console.WriteLine();
            return "MOVE_OR_PICK";  // Don't change action
        }

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

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






To just expand, instead of printing of the list items like it should. it prints out namespace.class name which in this case is SimpleTextAdventure.Inventory...




HELP!!!!!

解决方案

you are iterating through _inventory list of Inventory Items. You are calling each one 'itemName' but it isn't the item name. so each itemName is really an Inventory object so you are getting the '.ToString()' of the object not the ItemName value which you want.

make the change below.

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


You have to provide member name you want to print. In your loop what you named as "itemName" is the instance of inventory class. Name you chose is misleading.


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




Another way (without changing your loop when you print to console) is to override ToString() method in inventory class

public override string ToString()


这篇关于无法获取列表打印..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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