好的,基本上,我正在为一个课程项目制作一个随机项目生成器。 现在名称生成部分一旦命中3个或更多名称就不起作用。 我得到的错误是:
FormatExeption未处理 类型' System.FormatException'的未处理异常发生在mscorlib.dll
附加信息:索引(从零开始)必须大于或等于零且小于参数列表的大小
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()
发生答案 0 :(得分:3)
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}..."
等等。
答案 1 :(得分:1)
您正在以字符串格式跳过{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或更多时才有意义。