随机物品生成器错误
本文关键字:错误 随机 | 更新日期: 2023-09-27 18:16:02
基本上,我正在为一个类项目创建一个随机项生成器。现在,一旦遇到3个或更多的名字,名称生成部分就不能工作了。我得到的错误是:
formatexception was Unhandled未处理的System类型异常。mscorlib.dll中出现FormatException
附加信息:索引(从零开始)必须大于或等于零且小于实参列表的大小
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
public static string ID { get; set; }
public static int Damage { get; set; }
public static int Defense { get; set; }
public static string Name;
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 List<string> Value1 = new List<string> { "Wooden", "Bronze" };
public static List<string> Value2 = new List<string> { "Silver", "Steel" };
public static List<string> Value3 = new List<string> { "Dragon Glass", "Adamantium", "Titanium", "Arcane", };
public static List<string> ArmorValue = new List<string> { "Cloth", "Leather", "Iron", "Silver", "Steel", "Titanium", "Arcane", "Dragon Glass", "Titanium" };
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 static string ItemGenerator(int droprarity)
{
int dropRarity = droprarity;
NumberOfNames = NumberOfNamesGenerator(dropRarity);
Console.WriteLine(NumberOfNames);
Rarity = RarityGenerator(dropRarity);
ItemType = ItemTypeGenerator();
if (ItemType == "Sword")
{
ItemType = WeaponList[Random.Next(0, WeaponList.Count)];
}
Name = NameGenerator();
return Name;
}
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}", Rarity, ItemType);
}
else if (nameNR == 3)
{
return string.Format("{0} {1} of {3}", Rarity, ItemType, NameLenght3List[Random.Next(0, NameLenght3List.Count)]);
}
else if (nameNR == 4)
{
return string.Format("{0} {1} of {3} {4}", Rarity, ItemType, Name1[Random.Next(0, Name1.Count)], Name2[Random.Next(0, Name2.Count)]);
}
else if (nameNR == 5)
{
return string.Format("{0} {1} of {3} {4} {5}", Rarity, 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 {3} {4} {5} {6}", Rarity, 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;
}
}
static void Main(string[] args)
{
int number;
number = Random.Next(1, 6);
Name = ItemGenerator(number);
Console.WriteLine(Name);
Console.ReadLine();;
}
}
}
错误发生在公共静态字符串NameGenerator()
else if (nameNR == 3)
{
return string.Format("{0} {1} of {3}", Rarity, ItemType, NameLenght3List[Random.Next(0, NameLenght3List.Count)]);
}
else if (nameNR == 4)
{
return string.Format("{0} {1} of {3} {4}", Rarity, ItemType, Name1[Random.Next(0, Name1.Count)], Name2[Random.Next(0, Name2.Count)]);
}
else if (nameNR == 5)
{
return string.Format("{0} {1} of {3} {4} {5}", Rarity, 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 {3} {4} {5} {6}", Rarity, 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)]);
}
在所有这些string.Format()
方法中,您跳过{2}
而从{1}
跳到{3}
,这是不允许的。将它们改为return string.Format("{0} {1} of {2}..."
等
您正在跳过字符串格式中的{2}位置。我想这就是问题所在。
的例子:
return string.Format("{0} {1} of {2}", Rarity, ItemType, NameLenght3List[Random.Next(0, NameLenght3List.Count)]);
不像你有它:
return string.Format("{0} {1} of {3}", Rarity, ItemType, NameLenght3List[Random.Next(0, NameLenght3List.Count)]);
enter code here
这就解释了为什么它出现在3或更多