从列表中获取项目,然后在控制台中编写其变量

时间:2015-10-06 14:25:55

标签: c# console.writeline

所以首先我生成项目,然后将它们添加到列表中。 但我不知道如何从列表1中逐一获取该项目,并说明其变量。 以下是将项目添加到列表

的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication4
{
    class Program
      {
        public static Random Random = new Random();
        static void Main(string[] args)
        {
          List<Item> Items = new List<Item>();
          int itemAmmount;
          Console.WriteLine("Please enter the amount of items you want.");
          itemAmmount = Convert.ToInt32(Console.ReadLine());
          int DropRarity;
          int ItemCount;
          int i; 

          for(i = 0; i < itemAmmount; i++)
          {               
              DropRarity = Random.Next(1, 6);
              Items.Add(new Item(DropRarity, i));
          }
          for(int a = 0; a < itemAmmount;)
          {
            string Name1 = Items.Select(C => C.Name);
            int Damage =
            int Health =
            int Mana =
         }
    }
}
}

这是项目生成器的代码

namespace ConsoleApplication4
{
class Item
{        
    public static string ID { get; set; }
    public static int Damage { get; set; }
    public static int Defense { get; set; }
    public static string Name;
    public static int HP, MANA;
    public static string Rarity;
    public static int NumberOfNames;
    public static string ItemType;
    public static Random Random = new Random();
    public static List<string> ItemTypeList = new List<string> { "Chest area armor", "Weapon", "Shield", "Head area armor", "Leggings", "Shoes", "Gloves", "Belt", "Ring" };
    public static List<string> WeaponList = new List<string> { "Staff", "Sword", "Dagger", "Wand", "Axe", "Mace" };
    public static string WeaponType;
    public static string Value;
    public static List<string> DamageType = new List<string> { "Meele", "Magic" };
    public static List<string> WeaponListMeele = new List<string> { "Staff", "Wand" };
    public static List<string> WeaponListMagic = new List<string> { "Sword", "Dagger", "Axe", "Mace" };
    public static List<string> ValuePoor = new List<string> { "Wooden", "Bronze" };
    public static List<string> ValueCommon = new List<string> { "Bronze", "Iron" };
    public static List<string> Value2 = new List<string> { "Silver", "Steel", "Iron" };
    public static List<string> ValueElite = new List<string> { "Silver", "Steel", "Dragon Glass", "Adamantium", "Titanium", "Arcane" };
    public static List<string> ValueLegendary = new List<string> { "Dragon Glass", "Adamantium", "Titanium", "Arcane" };
    public static List<string> ArmorValuePoor = new List<string> { "Cloth", "Leather" };
    public static List<string> ArmorValueCommon = new List<string> { "Cloth", "Leather", "Iron" };
    public static List<string> ArmorValue2 = new List<string> { "Iron", "Silver", "Steel" };
    public static List<string> ArmorValueElite = new List<string> { "Silver", "Steel", "Titanium", "Arcane", "Dragon Glass", "Titanium" };
    public static List<string> ArmorValueLegendary = new List<string> { "Titanium", "Arcane", "Dragon Glass", "Adamantium" };
    public static List<string> Name1 = new List<string> { "Stupid", "Great", "Powerful", "Epic", "Masterfull", "Mighty", "Bountiful", "Foreverlasting", "Pointless", "Useless" };
    public static List<string> Name2 = new List<string> { "Things", "Power", "Proportions", "Torment", "Legs", "Turnament", "Shit", "Crazyness", "Awesomeness" };
    public static List<string> NameLenght3List = new List<string> { "Ragnarok", "Power", "Bondage", "Noobs", "Great Weakness", "Error", "404", "Torture", "SHIT STATS(No idea since I'm not good enough to programm stats based on names)", "The Dark Lord", "Emperor", "Monkey Kong", "I ran out of words", "Blablabla" };

    public Item(int droprarity, int i)
    {
        int dropRarity = droprarity;
        NumberOfNames = NumberOfNamesGenerator(dropRarity);
        Rarity = RarityGenerator(dropRarity);
        ItemType = ItemTypeGenerator();
        if (ItemType == "Sword")
        {
            WeaponType = DamageType[Random.Next(0, DamageType.Count)];

            if (ItemType == "Meele")
            {
                ItemType = WeaponListMeele[Random.Next(0, WeaponListMeele.Count)];
            }
            else
            {
                ItemType = WeaponListMagic[Random.Next(0, WeaponListMagic.Count)];
            }
        }
        if (WeaponList.Contains(ItemType))
        {
            Value = WeaponValueGenerator(dropRarity);
            Damage = WeaponDamage(dropRarity);
        }
        else
        {
            Value = ArmorValueGenerator(dropRarity);
            HP = ArmorHealthGen(dropRarity);
            int RandomChance = Random.Next(0, 100);
            if (RandomChance <= 75)
            {
                MANA = ArmorManaGen(dropRarity);
            }
        }
        Name = NameGenerator();
        ID = i;


    }
    public static int IDGenerator
    public static string RarityGenerator(int dropRarity)
    {
        switch (dropRarity)
        {
            case 1:
                Rarity = "Poor";
                break;
            case 2:
                Rarity = "Common";
                break;
            case 3:
                Rarity = "Uncommon";
                break;
            case 4:
                Rarity = "Elite";
                break;
            case 5:
                Rarity = "Rare";
                break;
            case 6:
                Rarity = "Legendary";
                break;
        }

        return Rarity;
    }
    public static string ItemTypeGenerator()
    {
        return string.Format("{0}", ItemTypeList[Random.Next(0, ItemTypeList.Count)]);
    }
    public static int NumberOfNamesGenerator(int dropRarity)
    {
        switch (dropRarity)
        {
            case 1:
                NumberOfNames = 2;
                break;
            case 2:
                NumberOfNames = Random.Next(2, 4);
                break;
            case 3:
                NumberOfNames = Random.Next(2, 4);
                break;
            case 4:
                NumberOfNames = Random.Next(2, 5);
                break;
            case 5:
                NumberOfNames = Random.Next(2, 5);
                break;
            case 6:
                NumberOfNames = Random.Next(2, 6);
                break;
        }
        return NumberOfNames;
    }
    public static string NameGenerator()
    {
        int nameNR = NumberOfNames;
        if (nameNR == 2)
        {
            return string.Format("{0} {1} {2}", Rarity, Value, ItemType);
        }
        else if (nameNR == 3)
        {
            return string.Format("{0} {1} of {2} {3}", Rarity, Value, ItemType, NameLenght3List[Random.Next(0, NameLenght3List.Count)]);
        }
        else if (nameNR == 4)
        {
            return string.Format("{0} {1} of {2} {3} {4}", Rarity, Value, ItemType, Name1[Random.Next(0, Name1.Count)], Name2[Random.Next(0, Name2.Count)]);
        }
        else if (nameNR == 5)
        {
            return string.Format("{0} {1} of {2} {3} {4} {5}", Rarity, Value, ItemType, Name1[Random.Next(0, Name1.Count)], Name1[Random.Next(0, Name1.Count)], Name2[Random.Next(0, Name2.Count)]);
        }
        else if (nameNR == 6)
        {
            return string.Format("{0} {1} of {2} {3} {4} {5} {6}", Rarity, Value, ItemType, Name1[Random.Next(0, Name1.Count)], Name1[Random.Next(0, Name1.Count)], Name1[Random.Next(0, Name1.Count)], Name2[Random.Next(0, Name2.Count)]);
        }
        else
        {
            string ErrorSword = "Error Sword/Armor/blablabla";
            return ErrorSword;
        }
    }
    public static string WeaponValueGenerator(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                Value = ValuePoor[Random.Next(0, ValuePoor.Count)];
                break;
            case 2:
                Value = ValueCommon[Random.Next(0, ValueCommon.Count)];
                break;
            case 3:
                Value = Value2[Random.Next(0, Value2.Count)];
                break;
            case 4:
                Value = ValueElite[Random.Next(0, ValueElite.Count)];
                break;
            case 5:
                Value = ValueElite[Random.Next(0, ValueElite.Count)];
                break;
            case 6:
                Value = ValueLegendary[Random.Next(0, ValueLegendary.Count)];
                break;

        }
        return Value;
    }

    public static string ArmorValueGenerator(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                Value = ArmorValuePoor[Random.Next(0, ArmorValuePoor.Count)];
                break;
            case 2:
                Value = ArmorValueCommon[Random.Next(0, ArmorValueCommon.Count)];
                break;
            case 3:
                Value = ArmorValue2[Random.Next(0, ArmorValue2.Count)];
                break;
            case 4:
                Value = ArmorValueElite[Random.Next(0, ArmorValueElite.Count)];
                break;
            case 5:
                Value = ArmorValueElite[Random.Next(0, ArmorValueElite.Count)];
                break;
            case 6:
                Value = ArmorValueLegendary[Random.Next(0, ArmorValueLegendary.Count)];
                break;

        }
        return Value;
    }

    public static int WeaponDamage(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                Damage = Random.Next(5, 20);
                break;
            case 2:
                Damage = Random.Next(25, 45);
                break;
            case 3:
                Damage = Random.Next(50, 105);
                break;
            case 4:
                Damage = Random.Next(110, 200);
                break;
            case 5:
                Damage = Random.Next(210, 500);
                break;
            case 6:
                Damage = Random.Next(550, 1200);
                break;
        }
        return Damage;
    }

    public static int ArmorHealthGen(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                HP = Random.Next(5, 20);
                break;
            case 2:
                HP = Random.Next(25, 45);
                break;
            case 3:
                HP = Random.Next(50, 105);
                break;
            case 4:
                HP = Random.Next(110, 200);
                break;
            case 5:
                HP = Random.Next(210, 500);
                break;
            case 6:
                HP = Random.Next(550, 1200);
                break;
        }
        return HP;
    }

    public static int ArmorManaGen(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                MANA = Random.Next(5, 20);
                break;
            case 2:
                MANA = Random.Next(25, 45);
                break;
            case 3:
                MANA = Random.Next(50, 105);
                break;
            case 4:
                MANA = Random.Next(110, 200);
                break;
            case 5:
                MANA = Random.Next(210, 500);
                break;
            case 6:
                MANA = Random.Next(550, 1200);
                break;
        }
        return MANA;
    }
}

} 在这里输入代码

1 个答案:

答案 0 :(得分:0)

您可以使用Foreach

foreach(var item in items) 
{ 
//here you can get item one by one
}