随机项目生成器错误[重复]
Posted
技术标签:
【中文标题】随机项目生成器错误[重复]【英文标题】:Random Item generator error [duplicate] 【发布时间】:2016-01-02 10:07:12 【问题描述】:好的,基本上,我正在为一个班级项目制作一个随机项目生成器。 现在,一旦出现 3 个或更多名称,名称生成部分就无法工作。 我得到的错误是:
FormatExeption 未处理 mscorlib.dll 中发生了“System.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()
【问题讨论】:
只是好奇,这是游戏相关代码吗? 旁注:以后请确保在提问之前搜索错误消息并提供演示问题的最小代码(有关指导,请参阅minimal reproducible example)。 【参考方案1】: 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..."
等等。
【讨论】:
你打败了我。该死的快速打字机..大声笑 JK. 我现在感觉很盲目,谢谢:) 没有问题。我编写企业安全和银行应用程序,并被字符串格式索引问题所困扰。有时我们看到它是因为它在我们的脑海中是有意义的,而第三只眼睛就是你所需要的。编码的一部分。 :) @MartinRingenberg 请将其中一个答案标记为“答案”以结束问题。无论是我的还是凯西的,因为他们都说到点子上了。【参考方案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 或更多。
【讨论】:
FML 谢谢,所以尝试了编码,我已经是盲人了 XD以上是关于随机项目生成器错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章