>我有两个字符串列表(girlsLIST1 和 girlsLIST2(,其中我有 5 个字符串值,但是当我将这些值增加到 6 时,我得到一个例外这是我的代码。 现在,我根据问题变量的值(for循环运行(具有问题变量,并从列表中选择一些随机字符串,并将这些结果存储在girl1names和girl2names列表中。目前我的问题变量的值是 5,但如果我将我的 girlsLIST1 和 girlsLIST2 列表中的字符串增加到 5 以上,那么我就会得到一个异常。
List<string> girlsLIST1 = new List<string> { "Aliya", "Manal", "Ayman", "Laiba", "Lubna", "Aziza" };
List<string> girlsLIST2 = new List<string> { "Mehmoona", "Samra", "Saria", "Aleena", "Faiza","Mehwish" };
List<string> girl1names = new List<string>();
List<string> girl2names = new List<string>();
int GirlName;
int GirlName2;
if (addquestions == true)
{
//for (int i = 0; i <girlsLIST1.Count; i++)
//{
// int Girlsnames1 = random.Next(girlsLIST1.Count);
// GirlName = girlsLIST1[Girlsnames1];
//}
//for (int i = 0; i < girlsLIST2.Count; i++)
//{
// int Girlsnames2 = random.Next(girlsLIST2.Count);
// GirlName2 = girlsLIST2[Girlsnames2];
//}
var random = new Random();
int min = 2;
int max = 10;
for (int i = 0; i < questions; i++)
{
GirlName = random.Next(girlsLIST1.Count);
GirlName2 = random.Next(girlsLIST2.Count);
girl1names.Add(girlsLIST1[GirlName]);
girl2names.Add(girlsLIST2[GirlName2]);
}
var list = new List<string>
{
girl1names[GirlName] +" was playing basketball. " + random.Next(min, max) + " of her shots went in the hoop. " + random.Next(min, max) + " of her shots did not go in the hoop. How many shots were there in total?",
"The restaurant has " + random.Next(min, max) + " normal chairs and " + random.Next(min, max) +" chairs for babies. How many chairs does the restaurant have in total?",
"How many cookies did you sell if you sold " + random.Next(min, max) + " chocolate cookies and " + random.Next(min, max) + " vanilla cookies?",
girl2names[GirlName2] + " bought a big bag of candy. The bag had " + random.Next(min, max) + " blue candies, " + random.Next(min, max) + " red candies and " + random.Next(min, max) + " green candies. How many candies were there in total?",
girl1names[GirlName] + " had " + random.Next(min, max) + " books at home. He went to the library to take out " + random.Next(min, max) + " more books. He then bought 1 book. How many books does Billy have now?",
"The hobby store normally sells " + random.Next(min, max) + " trading cards per month. In June, the hobby store sold " + random.Next(min, max) + " more trading cards than normal. In total, how many trading cards did the hobby store sell in June?",
girl2names[GirlName2] + " has " + random.Next(min, max) + " pieces of gum to share with her friends. There wasn’t enough gum for all her friends, so she went to the store to get " + random.Next(min, max) + " more pieces of gum. How many pieces of gum does Adrianna have now?",
girl1names[GirlName] + " has " + random.Next(min, max) + " pieces of gum to share with her friends. There wasn’t enough gum for all her friends, so she went to the store and got " + random.Next(min, max) + " pieces of strawberry gum and " + random.Next(min, max) + " pieces of bubble gum. How many pieces of gum does Adrianna have now?"
};
genericfunction(list);
//int index = randoms.Next(list.Count);
//DisplayAlert("word problems", list[index], "ok");
//Creates a new Word document
WordDocument wordDocument = new WordDocument();
//Adds new section with single paragraph to the document
wordDocument.EnsureMinimal();
wordDocument.LastSection.PageSetup.Margins.All = 15;
//Get Last paragraph of the document
IWParagraph paragraph = wordDocument.LastParagraph;
//Create a custom style
WParagraphStyle paragraphStyle = wordDocument.Styles.FindByName("Normal") as WParagraphStyle;
paragraphStyle.CharacterFormat.Font = new Syncfusion.Drawing.Font("Times New Roman", 20);
paragraphStyle.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Justify;
paragraphStyle.ParagraphFormat.AfterSpacing = 15f;
WSection section = wordDocument.LastSection;
for (int i = 0; i < wordproblemslist.Count; i++)
{
paragraph = section.AddParagraph();
string t = wordproblemslist[i];
paragraph.AppendText(t);
}
//Adds new text to the paragraph
//paragraph.AppendText(text);
//Adds first text to the paragraph
// paragraph = section.AddParagraph();
// paragraph.AppendText(text);
//Second paragraph
// paragraph = section.AddParagraph();
// paragraph.AppendText(text);
//Instantiation of DocIORenderer for Word to PDF conversion
DocIORenderer render = new DocIORenderer();
//Converts Word document into PDF document
PdfDocument pdfDocument = render.ConvertToPDF(wordDocument);
//Releases all resources used by the Word document and DocIO Renderer objects
render.Dispose();
wordDocument.Dispose();
//Save the document into memory stream
MemoryStream stream = new MemoryStream();
pdfDocument.Save(stream);
stream.Position = 0;
//Close the document
pdfDocument.Close();
//Save the stream into pdf file
//The operation in Save under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer PDF/Xamarin section for respective code samples.
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Output.pdf", "application/pdf", stream);
wordproblemslist.Clear();
girl1names.Clear();
girl2names.Clear();
private void genericfunction( List<string> list)
{
var random = new Random();
List<int> listNumbers = new List<int>();
int number;
for (int j = 0; j < questions; j++)
{
do
{
number = randoms.Next(list.Count);
} while (listNumbers.Contains(number));
listNumbers.Add(number);
}
for (int k = 0; k < listNumbers.Count; k++)
{
wordproblemslist.Add(list[listNumbers[k]]);
}
}
您以这种方式初始化GirlName
:
GirlName = random.Next(girlsLIST1.Count);
所以在这里,GirlName
将介于0
和5
之间。
然后,您尝试以这种方式从girl1names
中获取名称:
girl1names[GirlName]
但是,girl1names
只包含questions
名称,因为您以这种方式填写了此列表:
for (int i = 0; i < questions; i++)
{
girl1names.Add(girlsLIST1[GirlName]);
}
因此,如果碰巧GirlName
大于或等于questions
,你就出局了。
(girl2names[GirlName2]
也是如此(
溶液
不要尝试访问girl1names[GirlName]
。
使用不会超过girl1names
长度的索引(也许重新考虑您真正想要达到的索引,因为GirlName
不太可能是您要查找的索引(
girl2names
也一样。
原因:
您得到的异常表明Index Was Out Of Range. Must Be Non Negative And Less Then The Size Of Collection
意味着您要获取的对象索引超出了范围。
它发生在这里:girl1names[GirlName]
和girl2names[GirlName2]
.
girl1names
:这是一个保存名称的列表。其计数取决于问题value
。
GirlName
:在这里按Index
工作。它的最大值取决于girlsLIST1.Count。
为了防止出现异常,应确保 GirlName
的值小于 girl1names
的长度。
所以,这意味着,你定义的girlsLIST1.Count的值应该小于问题。(同girl2names[GirlName2]
(
我猜你的问题值小于 4,所以如果你将 5 个字符串值增加到 6,它会崩溃。
溶液:
确保问题的价值大于 girlsLIST1.Count 和 girlsLIST2.Count。